Render View From String And Array Of Macroses


I need to render a string from another string with macroses, using values from AR model. For example:

$template = 'Hello, {{username}}! Welcome to {{}}. {{message}}'; // Syntax does not matter: %variable%, {{variable}} etc.

$macroses = [





$model = new TestModel;

$result = (new SomeComponent)->render($template, $macroses, $model);

/* All entries of macroses must be replaced with corresponding model attributes, its related record attributes or getter result:

{{username}} must be replaced with $model->username (attribute value)

{{}} must be replaced with $model->application->name (related record attribute value)

{{message}} must be replaced with $model->getMessage() (result of getter method)


The question is how to implement this with Yii2 built-in capabilities? Maybe I can somehow use Twig/Smarty renderers to replace macroses? How can I get an array of values from ActiveRecord, using such syntax (‘’ for relations, ‘message’ for getters etc)?

For the last question I found only this solution:

$values = array_map(function ($key) use ($model) {

    try {

        return ArrayHelper::getValue($model, $key);

    // To resolve situations with unexisting attributes/relations/getters

    } catch (\Exception $e) {

        return null;


}, array_combine($macroses, $macroses));

But I think there must be much beautiful way :)

I think you have it right. The Yii2 Gridview column configuration is very similar to what you have and the values are parsed almost similarly using the DataColumn.

So, there is no built-in method for getting such data… Ok.

What about render? Is there a way to use rendering methods with string (not a file)?

Normally echo / return strings (render being a method for view files). Having said that, based on your design, you may want to use templates (Twig/Smarty) as well.

Just a thought… something as simple as strtr OR Yii::t could be used to parse strings and tags within strings in one statement.

For replacing params I can use also preg_replace_callback. My questions is about built-in template renderers (Twig/Smarty): can I use them for this? Replacing with preg_replace_callback or strtr is simple solution, of course, but with template renderers I can use not only param replacing, but also some logic (iterations, conditions and so on).


I had a problem with Twig…

I use it in advanced yii2, i did as mentionned in the doc. but when I see the page, i get {{ myVar }} etc …

Seems twig not interpreted.

Dou you see a raison to that ?

I put "yiisoft/yii2-twig": "*" in composer require, i did a composer update

I put

'twig' => [

            'class' => 'yii\twig\ViewRenderer',

            'cachePath' => '@runtime/Twig/cache',

            // Array of twig options:

            'options' => [

                'auto_reload' => true,


            'globals' => ['html' => '\yii\helpers\Html'],

            'uses' => ['yii\bootstrap'],


in main.php in backend (i use it here)

Thx in advance