Is any way to use twig in php? - php

All of my files are in PHP and now I have problem, because I have to use twig code on my PHP page:
{% block fos_user_content %}
{% endblock fos_user_content %}
Is there any way to include .twig file in .php file? Or does somebody know how to convert this twig code to PHP? Or maybe somebody know some tutorial for FOSUserBundle which is written in PHP?

I dont know why you trying to do, but i'll try to help you :
you can't include twig in php(2 engine for templating, why?)
you can "use php code" in your twig template with twig extension(but please read doc as #Yenne Info said)
You can render twig template with controller method renderView($template, $params)
But do you really use symfony2?

Related

How to run php code in Twig template file [duplicate]

I have a do_shortcut and I need to embed it in a twig template. I tried by coping the code in a php file my-code.php:
<?php do_shortcut('[my-code]'); ?>
Next, in the twig page over.twig:
{{ include ('options/my-code.php') }}
/* I also tried */
{% php %}
<?php do_shortcut('[my-code]'); ?>
{% endphp %}
But doesn't work. Any suggestion? Thanks.
You can't do that, you should create a twig extension and transform the php function into a twig function: http://symfony.com/doc/current/cookbook/templating/twig_extension.html
About the include part, create a my_code.html.twig file at app/Resources/views/my_code.html.twig and copy-paste your code from my-code.php
Then you can include that code anywhere like :
{% include 'my_code.html.twig' %}
EDIT: tested and working in symfony3
Try this code:
{{ wp.do_shortcode('[shortcode]')|raw }}

Including id element, or variable, from another TWIG template

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

Twig Templates not extending

I have just setup a apache server and configured twig. I have two template files one called base.html.twig which has the core template elements and dashboard with blocks that correspond to the base.html.twig
dashboard.html.twig
{% extends "base.html.twig" %}
{% block content %}
<h1>Index</h1>
<p class="important">
Welcome on my awesome homepage.
</p>
{% endblock %}
However when I browse to dashboard.html.twig I just see plain html showing all of the code above. Have I misconfigured something?
I am sure it's something simple so I thank you for your help in advance.
You should not directly "browse" to a twig file, it needs to be parsed and rendered by php objects using classes from Twig libraries.
see http://twig.sensiolabs.org/documentation
To use twig you need PHP installed too. Also you would need Twig PHP library to parse twig templates.

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 }}

Layout and content pages for pure html [duplicate]

This question already has answers here:
How to create a template in HTML?
(3 answers)
Closed 9 years ago.
I have worked on several ASP.NET applications recentely, and I was simply fascinated by the simplicity of it's HTML layouting.
Basiccaly there is one layout file, in which you write all the static HTML, CSS, JS, or really anything that will stay the same on all pages. Then, there are content pages, in which you only write the content of the pages.
So, for example you have an about.cshtml file that looks like this:
<h1>My name</h1>
<p>I am a junior web developer... etc</p>
<p>Contact me at example#gmail.com</p>
That's all the information needed for the about content page, when requested it will be inserted in the layout page.
So, my question:
Are there any ready-to-use tools to make this happen via html, javascript or php? I found myself implementing it via ajax calls, but that really is just a waste of resources, having to async load all the html files I have.
For pure HTML, you could go down the route of using a static site generator. For example:
Jekyll
Pelican
In Pelican you have a base.html template, which contains everything that you want to appear on all pages, so the header and footer etc (These can also be called .header.html and then referenced in the main.html template.
Page content is written in Markdown, and put into folders called posts or pages, and then you run a generate command, and it will output all the html for you.
And pages can be written like this:
{% extends "base.html" %}
{% block title %}{{ page.title }}{%endblock%}
{% block content %}
<h1>{{ page.title }}</h1>
{% import 'translations.html' as translations with context %}
{{ translations.translations_for(page) }}
{{ page.content }}
{% endblock %}
So it extends the base.html template.
Further reading:
Pelican: a static blog generator for
Pythonistas
Pelican - Getting
started
Check out Model–view–controllers

Categories