Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add a new test to check if a block exists #1821

Closed
hason opened this issue Sep 11, 2015 · 4 comments
Closed

Add a new test to check if a block exists #1821

hason opened this issue Sep 11, 2015 · 4 comments

Comments

@hason
Copy link
Contributor

hason commented Sep 11, 2015

Suggested names:

  • block name
  • block
  • defined block
{% if "content" is block name %}
<div>{{ block("content") }}</div>
{% endif %}
@ninsuo
Copy link
Contributor

ninsuo commented Sep 12, 2015

is block looks fair enough

@fabpot
Copy link
Contributor

fabpot commented Sep 12, 2015

I would use is block as well. Can you tell us a bit more about the use case? It is mainly useful for dynamic uses?

@hason
Copy link
Contributor Author

hason commented Sep 14, 2015

I want to use it in combination with tag embed:

{# header.html.twig #}
<h{{ level|default(2) }} class="ui{% if 'icon' is block %} icon{% endif %} header">
  {{ block('icon') }}
  {% if 'subheader' is block %}
    <div class="content">
      {{ block('header') }}
      <div class="sub header">{{ block('subheader') }}</div>
    </div>
  {% else %}
    {{ block('header') }}
  {% endif %}
</h{{ level|default(2) }}>

Example of use:

{# index.html.twig #}
{% embed 'header.html.twig' with { level: 3 } %}
    {% block header %}My settings{% endblock %}
    {% block icon %}<i class="settings icon"></i>{% endblock %}
{% endembed %}
{# <h3 class="ui icon header"><i class="settings icon"></i>My setting</h3> #}

{% embed 'header.html.twig' with { level: 4 } %}
    {% block header %}My header{% endblock %}
{% endembed %}
{# <h4 class="ui header">My setting</h4> #}

{% embed 'header.html.twig' with { level: 2 } %}
    {% block header %}My header{% endblock %}
    {% block subheader %}My subheader{% endblock %}
{% endembed %}
{# <h2 class="ui header"><div class="content">My setting<div class="sub header">My subheader</div></div></h2> #}

@fabpot
Copy link
Contributor

fabpot commented Sep 14, 2015

ok, can you submit a PR?

fabpot added a commit that referenced this issue Nov 11, 2016
This PR was merged into the 1.x branch.

Discussion
----------

Add "is defined" support for block()

replaces #1831, fixes #1821

I think reusing the semantic of the `defined` test is more idiomatic and easily discoverable.

/cc @hason

Commits
-------

3ce06af added 'is defined' support for block()
4f013e0 Add test to check if a block exists
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

No branches or pull requests

3 participants