Forum

Thread tagged as: Question, Add-on-development

Passing processed data from perch:content to template handler

Hi there,

I'm in the midst of creating my first mini add-on for Perch – thanks Hussein from pipits for your code samples! It's pretty basic and aim at decluttering templates from <perch:if> when handling multi-language so that I can do this <perch:translate id="translated" en="More videos" fr="Plus de vidéos" /> instead of using <perch:if> in templates.

I got this working when strings are passed to the attributes. It doesn't though work for content coming from <perch:content />. <perch:translate id="translated" en="<perch:content type="text" id="title_en" />" fr="<perch:content type="text" id="title_fr" />" />

Here is what the render method from the template handler looks like:

public function render($vars, $html, $Template) {
        $translatedValues = array();
        $tags = $Template->find_all_tags('translate');

        foreach($tags as $Tag) {
          $translatedValues[$Tag->id] = $Tag->$lang;
        }

        $html = $Template->replace_content_tags('translate', $translatedValues, $html);
      }

      return $html;
    }

Printing $Tag would return:

PerchXMLTag Object
(
    [attributes] => Array
        (
            [id] => title_en
            [en] =>  Array
        (
        )

    [tag:PerchXMLTag:private] => 
)

Is this something that can be done? Is there a way to pass over the processed data? I would appreciate if someone could point me in the right direction.

Thanks!

Robin Pick

Robin Pick 5 points

  • 3 years ago
Drew McLellan

Drew McLellan 2638 points
Perch Support

perch_content() runs the template and caches the output at edit time. If you need your tags to be late-parsing, you can use render_runtime() instead of render().