We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Issue with Volt macros

I tring to use macros

{%- macro button(link,text) %}
<a href='{{link}}' class='btn btn-default'>{{text}}</a>
{%- endmacro %}
{{ button('link','te<b>st</b>') }}

It works perfect. But if I try to move forward and write

{%- macro button1(link,text) %}
<a href='{{link}}' class='btn btn-default'>{{text|e}}</a>
{%- endmacro %}
{{ button1('link','te<b>st</b>') }}

I have error. Because no $this is defined inside macros function. WTF

Volt generates following code

function vmacro_button1 ($__p) { /* check args */
  echo $this->escaper->escapeHtml($text); // $this is not defined here
}
// ...
echo vmacro_button1(array('link', 'te<b>st</b>'));

Possible construction to define $this in function could look like

global $g_vmacro_button1; $g_vmacro_button1=function($__p) { /* check args */
  echo $this->escaper->escapeHtml($text);
};
Closure::bind($g_vmacro_button1,$this);
function vmacro_button1($__p) { global $g_vmacro_button1; $g_vmacro_button1($__p);  }
// ...
echo vmacro_button1(array('link', 'te<b>st</b>'));

The question is how to change the former macros into latter?



575
Accepted
answer

Sorry. I found an answer here https://github.com/phalcon/cphalcon/issues/1176 My phalcon was 2.0.1

andresgutierrez commented on 25 Jun 2015

This is fixed in Phalcon 2.0.4

Mark your second answer as correct to solve the thread so others can use it if needed :)