twig

Best practice for PHP paths [duplicate]

主宰稳场 提交于 2021-02-07 04:56:24
问题 This question already has answers here : PHP include best practices question (10 answers) Closed 7 years ago . I have been all over the Internet trying to figure out the best way to handle paths in my website. Should I use relative paths, absolute paths? I have seen dirname ( FILE ) mentioned several times. One problem I have with relative paths is that PHP files which are included by several other files at different directory levels cause the relative paths to break. For example if the

Twig Access Array Index?

我的梦境 提交于 2021-02-06 14:20:38
问题 Is it possible to directly access an array index from within a Twig template? Here's my setup, using Silex: return $app['twig']->render('template', array('numbers' => array('one', 'two', 'three'))); so can I do something like this? {{numbers[0]}} 回答1: Just before posting this I realized, that's exactly what you can do, but as I didn't find the answer anywhere in the docs or google (correct me if I'm wrong), I've posted this anyway. {{numbers[0]}} 回答2: The answer of Adam, is correct, only to

Symfony 'trans' domain inside Twig template

强颜欢笑 提交于 2021-02-06 09:41:05
问题 I'd like to do this: $this->get('translator')->trans('notice.unregistered', array(), 'index'); Inside Twig template, so I don't have to pass this as an argument. How? 回答1: You can also do using trans filter : {{ 'translationkey'|trans({},'domain') }} 回答2: The solution is: {% trans from "domain" %}text{% endtrans %} 回答3: You can add custom functions to change domains inside your templates. Add your functions: $getTextdomain = new Twig_SimpleFunction('get_textdomain', function () { return

Symfony translation with placeholder doesn't work

倖福魔咒の 提交于 2021-02-05 09:13:26
问题 I'm trying to translate my app with Symfony translate (5.1.*). I use Symfony 5.1. Basic translation work fine but I have problem with variable in twig. When I do {% trans with {'%name%': 'World'} from 'app' %}Hello %name%{% endtrans %} It works fine and the result is Hello World as expected. But if I do php bin/console translation:update --force en php bin/console cache:clear to generate the translation file, the result is Hello %name% . If in the translation file, I delete this reference:

twig dynamic variable call

不想你离开。 提交于 2021-02-05 05:31:18
问题 I passed data in 3 languages to the twig template and display this data in this way: {% set lang=app.request.get("lang")%} {% for item in contests%} {% if lang=="fa"%} {{item.titlefa}} {% elseif lang=="en"%} {{item.titleen}} {% elseif lang=="ar"%} {{item.titlear}} {% endif%} {% endfor%} It is wirking but I must create 3 if condition for each object in "contests" How can i show data in this logic: {% set lang=app.request.get("lang")%} {{item.title~lang}} {% endfor%} that can call proper method

twig dynamic variable call

无人久伴 提交于 2021-02-05 05:29:11
问题 I passed data in 3 languages to the twig template and display this data in this way: {% set lang=app.request.get("lang")%} {% for item in contests%} {% if lang=="fa"%} {{item.titlefa}} {% elseif lang=="en"%} {{item.titleen}} {% elseif lang=="ar"%} {{item.titlear}} {% endif%} {% endfor%} It is wirking but I must create 3 if condition for each object in "contests" How can i show data in this logic: {% set lang=app.request.get("lang")%} {{item.title~lang}} {% endfor%} that can call proper method

Equivalent of is_array in Twig

老子叫甜甜 提交于 2021-02-04 18:09:45
问题 I'm working on a template and I need to check if something is an array. How do I do that in Twig? I've tried {% if my_var is iterable %} {% for v in my_var %} ... {% endfor %} {% else %} {{ my_var }} {% endif %} but it always prints my_var, even when my_var is really an array, as evidenced when it prints out Array Array myusername ../data/table.sqlite3 回答1: Another way : {% if my_var.count()>1 %} 回答2: Just add a custom filter: $twig->addFilter('is_array', new \Twig_Filter_Function('is_array')

Equivalent of is_array in Twig

a 夏天 提交于 2021-02-04 18:09:13
问题 I'm working on a template and I need to check if something is an array. How do I do that in Twig? I've tried {% if my_var is iterable %} {% for v in my_var %} ... {% endfor %} {% else %} {{ my_var }} {% endif %} but it always prints my_var, even when my_var is really an array, as evidenced when it prints out Array Array myusername ../data/table.sqlite3 回答1: Another way : {% if my_var.count()>1 %} 回答2: Just add a custom filter: $twig->addFilter('is_array', new \Twig_Filter_Function('is_array')

Generate Twig Template from Twig Template? (CodeGenerator)

我怕爱的太早我们不能终老 提交于 2021-01-29 14:40:17
问题 I write a code generator in Symfony2 with twig. My problem is: I want to create a twig template with a twig template. For a better understanding here an example: I have a twig file that is a template for a PHP Page - so if i run it it generates me PHP code from that twig template. (ex. a Controller for CRUD ) Now I want to generate the view template - but how can i tell twig to use the commands I need for generation and let the dynamic parts for the template as is? Can I change how the tags

Twig variables as references

生来就可爱ヽ(ⅴ<●) 提交于 2021-01-29 09:16:01
问题 I'm trying to change a Twig variable via a php reference but I can't achieve that. I looked around and nor with Twig functions nor with Twig filters I could do what I want. Any idea how to do that? {% set hiding_options_classes = "default" %} {{ hiding_options_func(content.field_hiding_options, hiding_options_classes) }} {{ hiding_options_classes }} In my Twig extension file: public function hiding_options_func($hiding_options, &$hiding_options_classes) { $hiding_options_classes = "coucou"; }