I want to be able to pass into twig some html in a variable and render it as
{{ data.content | raw }}
But within that content variable I would like to be able to have content like this - pulled from a database:
<div>
<p>Text with a Twig Extension function call eg: {{ doSomething('112233') }}</p>
</div>
Is there any way to get that twig extension to fire having come from the Twig variable itself?
Thank you.
There is one possible solution. Look at template_from_string twig function. It says:
New in version 1.11: The template_from_string function was added in Twig 1.11.
The template_from_string function loads a template from a string:
{{ include(template_from_string(data.content)) }}
But doc also says:
The template_from_string function is not available by default. You must add the Twig_Extension_StringLoader extension explicitly when creating your Twig environment.
So, probably it won't work out of box.
Related
I'm working on a site where i in twig renders content that gets set via ACF-fields. Currently i'm implementing translations and was wondering how i would do it since it not really strings, its twig.
Php file:
$context = Timber::get_context();
$context['header'] = array(
'title' => get_field('header_title')
);
Timber::render('/templates/index.twig', $context);
My template looks like this.
<header>
{% if header.title %}
<h1>
{{ header.title }}
</h1>
{% endif %}
</header>
But for the translation tool (po files) want the syntax to be:
{{ __("string to translate") }}
So how can i instead pass in {{ header.title }} into that?
You can’t pass the header title into __(). Only static strings that are written in your code are handled with gettext functions like __(). They don’t work with variables. If you have strings from the database, you won’t use string translation functions. Instead, you need a multilingual solution for WordPress.
Read the Codex page about Multilingual WordPress to get started. You’ll probably want to use a plugin if you want to have content that is pulled from the database translated. Among the popular ones are:
MultilingualPress
WPML
Polylang
i need to extends different base template in twig. this is possible? in my code i use dinamically render template with a controller like this
{{ render(controller('AppBundle:Default:menuManager')) }}
in that controller i use one action to choose the right template to render in my page, and this work fine. But in this case is different: i what change my base default (so i presume to extends it, right?) but i don't know how do this. Something like this?
{{ extends(controller('AppBundle:Default:baseManager')) }}
But this code don't work. Is possible? There is a different way? Thanks
This depends on the conditions for choosing the template base but you can use a twig extension. I use that in one project :
{% extends app.request.host | switchBaseTemplate %}
In this example, I use the host for the condition.
You can then write a twig extension easily, as explained here :
http://symfony.com/doc/current/templating/twig_extension.html
Have good dev.
To make an animated slider, I overwrite a fields view template file in my own template named: views-view-fields--slider.html.twig
Inside that file I have access to a fields array to print out all the fields that are in my content-type slide.
The problem is that I need to get the path of the image file instead of the image itself, because I need the path for CSS background-image styling.
The following doesn't work:
<div class="jumbotron " ref="" style="background-image:url(?????);">
{{ file_url(fields.field_image.entity.fileuri) }}
</div>
I tried everything I good find on Google:
fields.field_image.entity.uri
fields.field_image.entity.uri.value
fields.field_image.entity.url
fields.field_image.entity.url.value
etc.
Has anyone an idea how it is done in Drupal 8 ?
Since you are in views-view-unformatted you should already have access individualy to each field of your content type Slide with {{ row.content }} and because of that you have to do:
{{ file_url(row.content.field_image.entity.fileuri) }}
Try {{ file_url(row._entity.field_image.entity.uri.value) }}.
It works for me.
<div style="background-image:url({{ file_url(row._entity.field_image.entity.uri.value) }});"></div>
I found this module: image_url_formatter, so in View interface, I can use this to formatter my image filed as path.
It works perfect, but if I turned the twig debug on, it wouldn't work, because it will output debug annotation. I don't know how to solved it yet.
I use fields.field_image.content to show the path, I don't konw whether it's rignt.
Solution without extra module:
In your Views:
1.Add your image filed to the Advanced/Relationship
2.Then in the filed section, change to File Group, you can see URI field,add it, and make sure you check "Display the file download URI"
3.Add your new field in the twig template: {{ field.uri.content }}
Done!
Does anybody know how to get the current URI in a Twig template?
I’ve read through the documentation and I’m unable to find the Twig function to do this.
{{ app.request.uri }}
If you want to read it into a view variable:
{% set uri = app.request.uri %}
The app global view variable contains all sorts of useful shortcuts, such as app.session and app.security.token.user, that reference the services you might use in a controller.
I am (still) trying to introduce http://xoxco.com/clickable/jquery-tags-input into a dedicated bundle. As far, I have a type as a child of text and a data transformer that converts comma-separated strings into arrays of Objects and vice versa.
Now I want to decorate the text field with the JQuery code linked above. As far as I understand, I have to define a block like
{% block manytomanycomboselector_widget %}
{% spaceless %}
{{ block('text_widget') }}
<script>
$(function(){
$("#{{ id }}").tagsInput();
});
</script>
{% endspaceless %}
{% endblock manytomanycomboselector_widget %}
in [MyTypeBundle]Resources/views/Form/fields.html.twig
Now, both the documentation and the answers for this question at StackOverflow state that I have to reference fields.html.twig somewhere either in the template that uses the form or in app/, but this doesn't seem to be necessarily for other field-type bundles, though I cannot see in their code why.
What do I have to configure inside the bundle besides this block in this file?
Also I didn't get where I have to put the css and js requirements for the header and how I deal with general requirements like jQuery itself.
I have the same issue & I resolve it by merging my field template in the twig.form.resources parameter.
So, in the DI extension of my bundle (MyBundle/DependencyInjection/MyBundleExtension.php), I add:
$container->setParameter('twig.form.resources', array_merge(
array('MyBundle:Form:field_widget.html.twig'),
$container->getParameter('twig.form.resources')
));
Be aware, your bundle must be registered after the TwigBundle in your AppKernel.
EDIT:
A form field is not linked to any JS or CSS. So, IMO, you have 2 solutions.
Firstly, you directly wrap your JS & CSS in your field template and your bundle stays stand-alone.
Secondly, you instruct final users that they need to include manually some JSS & CSS each time they use your field type.
The IoFormBundle & GenemuFormBundle uses the second solution like explain in their documentation.