We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 3688
    • 43 Posts
    Hi,

    I have the following problem and can’t figure it out with the forums or the API-doc:

    I have different template-variables representing types of exercises, let’s say "exercises" and "homework". Assume they are strings (containing exercises and homework). Every article/document has one or some/all of them assigned to it.

    I have written an easy parser in PHP, let’s say it replaces all "asdfasdf" in strings with "SOME TEXT".

    Now I want that if the template-variable exists it is run through this parser and printed on the screen.

    My problem is that I don’t know how to call the parser many times. I did this:

    A) Create 2 different snippets "parse_exercises" and "parse_homework" with this code:
    // snippet parse_exercises
    $modx->setPlaceholder('name', 'exercises');

    so here i try to define a variable for using in the parser:

    B) Create a snippet called "parser" with content like this:
    $type_of_exercise = $modx->getPlaceholder('name');
    
    $value_of_tv = $modx->getTemplateVarOutput(array('$type_of_exercise'));
    
    			// if template-variable exists
    if(!empty($value_of_tv['$tv_uebungstyp']))
    {
    // parse it, e.g.
    $result = ...
    }
    $modx->setPlaceholder('parsed_text',$result);
    
    


    C) try to output the parsed text with a chunk "output" like
    <h1>exercise</h2>
    <p>[[+parsed_text]]


    D) In my template I first call the snippets to parse it and then the chunk to output it:
    [[parse_exercises]]
    [[parser]]
    [[$output]]
    
    [[parse_homework]]
    [[parser]]
    [[$output]]
    

    But well, nothing goes ...
    I feel there must be an easier way, but I just can’t work it out ...

    Would appreciate help very much, thanks in advance!
      • 15083
      • 697 Posts
      modx->runSnippet?
      http://wiki.modxcms.com/index.php/API:runSnippet
        • 3688
        • 43 Posts
        Ok, this is exactly what is working. I use code like this for it:
        $modx->runSnippet('nameOfSnippet', array('type_of_exercise' => 'homework'));

        The snippet contains code like this:
        $value_of_tv = $modx->getTemplateVarOutput(array('$type_of_exercise'));
        // $type_of_exercise is defined in the params of the runSnippet-API-command
        


        Thanks for your help!

        (my case closed)