Including id element, or variable, from another TWIG template - php

I have picked up the basics of using TWIG to create my site. I suppose I know how to use {%extend%} {%block%} {%include%} and {%set%}, in the most general sense.
I would like to include a block of code from within another twig file without including the whole file - as opposed to {% include 'file.twig' %}.
I have figured out how to set a variable in file.twig and output it using {{ variable | raw }}. I would like to do that in another file, like you would with using jQuery's .load function.
I swear the twig documentation does not seem to touch on this, it seems like really obvious and basic functionality. I have messed around with various combinations of include, for, with, in and only, colons and commas and whatever | is, and nothing.

I believe you are looking for horizontal inheritance via the use tag:
The use statement tells Twig to import the blocks defined in blocks.html into the current template (it's like macros, but for blocks)
The confusing part is that by itself, {% use ... won't actually insert the content of the blocks in the referenced template. To do that, you must use the block(...) function:
index.twig
{% use "blocks.twig" %}
{{ block('name') }}
blocks.twig
{% block name %}
<h1>Alex Weissman</h1>
{% endblock %}
{% block hobby %}
<p>Blanchin'</p>
{% endblock %}
For a working example, see my TwigFiddle (yes, it's a real thing!): http://twigfiddle.com/jjbfug

Related

How to dynamically load and display translations in TWIG from PHP variables?

I'm trying to make the trans('key' : value) method work by providing the variables either by PHP or preparing them on TWIG, both without any success so far.
So far I have tried to send the full line from the PHP Controller, which would be
{{bodymail| trans({'%user.name%': user.name,'%user.surname%': user.surname}) | raw}}
The following above works correctly when hard-coded, but when sending/composing it, it displays either the above code literally, or the correct message but without applying the translations.
This is something else I have tried, now on TWIG itself
{% set message = "'"~bodymail~"'| trans({" %}
{% for sub in variables %}
{% set message = message~"'%"~sub~"%': "~sub~"," %}
{% endfor %}
{% set message = message | trim (',', 'right') %}
{% set message = message~"}) | raw" %}
{{message}}
Code above is something I also tried for constructing the whole thing in TWIG itself... which resulted in directly not rendering the bodymail message (reference to .yml file).
Trying to make it work as a generic for anything that I input following the format (now i'm using user.name and user.surname, but I might want to use product references later for example).

Parse Text In Twig View In ZF2

I am designing the opening page of my personal web site. I am trying to use the same service function as the 'blog' section opening which retrieves the latest blog entry.
However I only want up to the 2nd paragraph to display on the opening page of my person web site. For me to re-use the service function I would need to parse after the second
</p>
within Twig. In other words I would want something like
<p>blah1 blah1 blah1</p>
<p>blah2 blah2 blah2</p>
to be the result. What syntax is needed in Twig?
You could do something like this...
Let's say that your text content is inside a twig variable named content.
Split your text content on every occurrence of </p>:
{% set contentArray = content|split('</p>') %}
First paragraph is everything inside first element of array after the first occurrence of <p>
{% set firstParagraphArray = contentArray[0]|split('<p>') %}
{% set firstParagraph = '<p>' ~ firstParagraphArray[1] ~ '</p>' %}
Second paragraph:
{% set secondParagraph = contentArray[1] ~ '</p>' %}
Output:
{% autoescape %}
{{ (firstParagraph ~ secondParagraph)|raw }}
{% endautoescape %}
Alternatively, you could make twig extension, that does all this job, and makes twig code a little bit cleaner.

Symfony2 turn autoescaping off in php template

There is way to turn autoescaping off in twig template in Symfony2. Like so:
{% autoescape false %}
{{ child.vars.label }}
{% endautoescape %}
How can I do something like this in PHP template? I got expanded choice type in my form, and I need to use images as labels. But Symfony keeps escaping my img tags.
Edit:
Sorry for misunderstood, but php templates have no autoescaping. If you want escaping in php templates you must do:
<?php echo $view->escape($var) ?>
Original answer:
In Twig use raw filter - http://twig.sensiolabs.org/doc/filters/raw.html
{{ child.vars.label | raw }}

how can I var dump a block/node content in drupal 8

I am trying to override block theme and rebuild it with html and twig.
I cant seem to find the variables from the block type or content type and cant find the image url for example.
how can i reach it using kint?
The easiest way to dump everything is with
{{ dump() }}
inside your twig template.
I work on fairly large Drupal sites, and I use this to not exhaust the memory from looping through vars.
<ol>
{% for key, value in _context %}
<li>{{ key }}</li>
{% endfor %}
</ol>
This will dump everything into a nice ordered list.
Hope this helps!
Also I'm not sure if you're already doing this, but if not -- turn on the twig debug tool, then check out your inspector of choice, and it'll give you suggestions and override data.
You can do this inside your sites/default/services.yml with
twig.config:
debug: true
If you have kint (of Devel module) installed, just use:
{{ kint(_context) }}
Its better than {{ dump() }}, cause kint can manage when the recursion is too long, avoiding memory issues. Second, have a nice way to display the information.

Dynamic {% use %} statement in twig?

I want to achieve the following thing:
I have a basic template for all of my pages, named "_page_base.twig". It contains the header and footer.
Then I have a Template for different areas of the page: "topic.twig", "section.twig" and "article.twig" - each of them extending the "_page_base.twig", thats working well so far.
Now I want to write my articles. I would love to save them as .twig file as well, since I can edit the complete markup in my editor and just upload it.
Since I can't say that my article files just extend "article.twig" (multiple inheritance is not possible) I could tell the "article.twig" that it should use blocks from my different article-content twigfiles.
The problem is: "use" statements have to be hardcoded!
My current solution is to add {% use "[PLACEHOLDER]" %} into the "article.twig" and then loading the template into a string, replacing the placeholder to the correct article-content.twig and then passing the whole thing to the template engine. What a mess.
Have you guys any idea for a better solution?
You can try with include tag. Since this tag accepts a dynamic name template to include, you just only need to define a variable which contains the name of article twig template.
{# on article.twig#}
{% set articles = ['someArticle.twig', ...] %}
{% for article in articles %}
{% include article %}
{% endfor %}
{# on someArticle.twig #}
... Article text ...
In the case of you need to customize some content inside of someArticle.twig you could stage the next level: embed tag. You should define a block tag inside of someArticle.twig, this block will be the placeholder to the custom values.
{# on article.twig#}
{% set articles=['someArticle.twig', ...] %}
{% for article in articles %}
{% embed article %}
{% block inside_text_article %}
... custom text ...
{% endblock %}
{% endembed %}
{% endfor %}
{# on someArticle.twig #}
... article text ...
{% block inside_text_article %}default values{% endblock %}
... article text ...
https://github.com/fabpot/Twig/issues/17 - no dynamic namespaces
LiipThemeBundle might be a solution:
http://symfony2bundles.org/liip/LiipThemeBundle
Unless I am not understanding the question, displaying a single article only requires that it also extends __page_base.twig.
In the case that you would need to display multiple articles, you could have a template dedicated to showing a list of articles which would also extend __page_base.twig and pass the article list to this template.

Categories