How to render content from string/database with twig?
see http://twig.sensiolabs.org/doc/functions/template_from_string.html
and http://symfony.com/doc/current/cookbook/templating/twig_extension.html#register-an-extension-as-a-service
{% include template_from_string("Hello {{ name }}") %}
{% include template_from_string(page.template) %}
Since the string loader is not loaded by default, you need to add it to your config.
# src/Acme/DemoBundle/Resources/config/services.yml
acme.twig.extension.loader:
class: Twig_Extension_StringLoader
tags:
- { name: 'twig.extension' }
Where Acme/acme is your application name and DemoBundle is the bundle you want to enable it for.
Unescape HTML string from database on twig template
I'm almost sure that HTML was escaped, so try it out:
<div id="desc">{{ projet.description|raw }}</div>
See raw filter on twig docs: raw
The raw filter marks the value as being "safe", which means that in an
environment with automatic escaping enabled this variable will not be
escaped if raw is the last filter applied to it
Also:
var desc = document.getElementById("desc").innerHTML|e('js')|raw;
The above snippet is not valid on twig because it isn't surrounded by valid delimiters such as "{{ }}
" or "{% %}
".
Twig render string variable as array
After some testing and brainstorming, I've decided to remove the twig renderer and migrate it to a more simple version.
Long story short, this will render all {{variable.something}} like syntax and will populate it with the corresponding value from the payload.
IF the value is an array, it will json encode it, and after that decode it, in order to achieve the requested functionality.
private function renderVariables(array $variables = [], array $payload = [])
{
$rendered = [];
foreach ($variables as $key => $value) {
if (is_array($value)) {
$rendered[$key] = $this->renderVariables($value, $payload);
} else {
$rendered[$key] = $this->replaceMarkdown($value, $payload);
if (strpos($rendered[$key], '[') !== false || strpos($rendered[$key], '{') !== false) {
$rendered[$key] = json_decode($rendered[$key]);
}
}
}
return $rendered;
}
public function replaceMarkdown(string $param, array $argv)
{
return preg_replace_callback('/\{\%\s*(.*)\s*\%\}|\{\{(?!%)\s*((?:[^\s])*)\s*(?<!%)\}\}/i',
function ($field) use ($argv) {
$field = $field[2];
if (strpos($field, '.') !== false) {
$fields = explode('.', $field);
} else {
$fields = array($field);
}
$currentData = $argv; # we should take care for magic before foreach
foreach ($fields as $currentField) {
if (isset($currentData[$currentField])) {
$currentData = $currentData[$currentField];
} else {
return $field . '-not-found';
}
}
if (is_array($currentData)) {
$currentData = json_encode($currentData);
}
return (string)$currentData;
}, $param);
}
Yii2 render database content
I found Mustache ( https://github.com/bobthecow/mustache.php ) and I'm using like this:
$m = new \Mustache_Engine();
echo $m->render("Hello {{model.client.firma}}",['model' => $model]);
Related Topics
Differencebetween $_Files["File"]["Type"] and End(Explode(".", $_Files["File"]["Name"]))
PHP MySQL SQL Parser (Insert and Update)
How to Ensure I Caught All Errors from MySQLi::Multi_Query
$_Post Empty on Utf-8 Characters
Prepared Statements - Are They Necessary
How to Count the Consecutive Duplicate Values in an Array
How to Replace Multiple Values in PHP
Call to Undefined Function Session_Register()
In PHP, Is There Any Harm in Running Session_Start() Multiple Times
Get/Set Dpi with PHP Gd/Imagick
Extending Session Timeout in PHP via the .Htaccess
PHP Error: Notice: Undefined Index:
Counting Values in Multidimensional Array