We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 9207 ☆ A M B ☆
    • 2,475 Posts
    Ok... I’m trying to figure out the View layer in MODx Revolution (I’m talking MVC speak here).

    What I’m trying to do: use Smarty to format Snippet output. I’m trying to figure out a way to parse a template file.

    Valid Snippet content is something like this, where "testChunk" is a Chunk I created:

    $output = $modx->getChunk('testChunk',array(  
       'name' => 'John',  
    ));  
    return $output;


    But the problem I have with this is that this LOADS and PARSES the Chunk. Not so fast... what if I want to provide a default text string and parse that?

    So in a normal MODx Revolution Snippet, the Smarty class is not included. You can include the Smarty class... but this seems incomplete because the normal Smarty methods don’t seem to work:
    include_once('/path/to/core/model/smarty/Smarty.class.php');
    $output .= $Smarty->fetch( '/path/to/your/sample.tpl' );
    return $output;
    


    This doesn’t work too good... not sure how I should do this... the page at http://rtfm.modx.com/display/revolution20/Custom+Manager+Pages doesn’t cover this...

    When I try the fetch or display methods, I get all kinds of errors in the MODx logs:

     [2010-08-22 22:55:54] (ERROR @ /path/to/my_modx/core/model/smarty/Smarty.class.php : 1092) User error: Smarty error: the $compile_dir 'templates_c' does not exist, or is not a directory.
    [2010-08-22 22:55:54] (ERROR @ /path/to/my_modx/core/model/smarty/Smarty.class.php : 1262) PHP warning: Smarty::include(templates_c/%%79^79C^79CD59B3%%sample.tpl.php) [<a href='smarty.include'>smarty.include</a>]: failed to open stream: No such file or directory
    [2010-08-22 22:55:54] (ERROR @ /path/to/my_modx/core/model/smarty/Smarty.class.php : 1262) PHP warning: Smarty::include() [<a href='function.include'>function.include</a>]: Failed opening 'templates_c/%%79^79C^79CD59B3%%sample.tpl.php' for inclusion (include_path='.:/Applications/MAMP/bin/php5/lib/php')
    


    You can see by the logs that I’m using MAMP... but WTF?
      • 4172
      • 5,888 Posts
      with this code-example you should be able to parse files, chunks or direct code as template source:

      <?php
      
      $items[] = array(  
         'name' => 'John',  
      );
      $items[] = array(  
         'name' => 'Tom',  
      );
      $output = '';
      if (substr($tpl, 0, 6) == "@FILE:")
      {
          $template = $this->get_file_contents($modx->config['base_path'].substr($tpl, 6));
      } else if (substr($tpl, 0, 6) == "@CODE:")
      {
          $template = substr($tpl, 6);
      } else if ($chunk = $modx->getObject('modChunk', array ('name'=>$tpl), true))
      {
          $template = $chunk->getContent();
      } else
      {
          $template = FALSE;
      }
      
      if ($template)
      {
          if (count($items) > 0)
          {
              foreach ($items as $item)
              {
                  $chunk = $modx->newObject('modChunk');
                  $chunk->setCacheable(false);
                  $output .= $chunk->process($item, $template);
              }
          }
      }
      
      
      return $output;
      ?>

        -------------------------------

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 26903
        • 1,336 Posts
        The modx object has an initialised Smarty object, try :-
         $modx->smarty->fetch('yourtemplate.tpl');

          Use MODx, or the cat gets it!
          • 9207 ☆ A M B ☆
          • 2,475 Posts
          @shamblett: the $modx object only seems to have the smarty object initialized if it’s used in the manager (e.g. a Custom Manager Page). When you try to use that object in a Snippet, it generates a fatal error because that object ISN’T instantiated:

          Fatal error: Call to a member function fetch() on a non-object in /path/to/core/cache/elements/modsnippet/14.include.cache.php on line 16


          That’s why I was trying to include the Smarty classes in my Snippet.

          @Bruno17: Thanks... where are the docs for the "process" method? I see this: http://rtfm.modx.com/display/revolution20/modX.getParser
            • 26903
            • 1,336 Posts
            Ah yes, forgot about that, if you find where this is done however it may point you in the direction as to what you need to do to set up your Smarty object, I’m sure it needs cache/compiler directories etc.
              Use MODx, or the cat gets it!
              • 9207 ☆ A M B ☆
              • 2,475 Posts
              You know... Bruno17.... I can create a Chunk object on the fly... it doesn’t have to be loaded from the database I don’t think...

              http://rtfm.modx.com/display/revolution20/Chunks
                • 30862
                • 14 Posts
                In case it’s still relevant to someone - how to initialize smarty manually:

                $modx->getService('smarty','smarty.modSmarty');
                  • 9207 ☆ A M B ☆
                  • 2,475 Posts
                  Thanks!