We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
    • 7914
    • 137 Posts
    I am trying to keep from repeating as much code/template as possible. I think in the end I will need 4 templates due to differences in navigation. That said, I want to re-use as much of the code as possible. So I thought I would render a page by building snippets. Then in my template page I would just call the snippet. That way if there was a change to the overall html, instead of editing 4 pages, I would only need to update the snippet that is generating the markup. -- Hopefully that made sense.

    All was working fine, I was getting the output I want until I stated to see if things like metadata were working. Below is an example of what I am using to generate the <head> of my template. I tried to remove as much as possible, hopefully it will still make some sense.

    MODx is generating all of the correct markup, but I am unable to set the metadata from the manager. Is it because I am asking MODx to render too many things and it’s in essence overwriting itself?

    If I type in the metadata code markup (html) into the template (html) it seems to work. Has anyone had any luck doing this? Or am I just S.O.L. and have to create 4 complete templates.

    Snippet "InsidePage":
    function RenderPage()
    {
    $html = '';
    $html .= '[[Head]]'."\n";
    
    // render the snippet to the page
    echo ( RenderPage() );
    





    Snippet: "Head"
    <?php
    // function to render the head of the template
    function RenderHead()
    {
    $html = '';
    $html .= '<head>'."\n";
    
    $html .= '<title></title>'."\n";
    $html .= '<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />'."\n";
    $html .= '<meta name="keywords" content="" />'."\n";
    $html .= '<meta name="description" content="" />'."\n";
    		
    $html .= '<link rel="stylesheet" type="text/css" media="all" href="'.LINK_CSS.'style.css" />'."\n";	
    $html .= '<link rel="stylesheet" type="text/css" media="print" href="'.LINK_CSS.'print.css" />'."\n";
    
    $html .= '<link rel="shortcut icon" href="'.IMAGES.'favicon.ico" />'."\n";
    		
    $html .= '<script language="javascript" type="text/javascript" src="'.LINK_JS.'menu.js"></script>'."\n";
    $html .= '<script language="javascript" type="text/javascript" src="'.LINK_JS.'DD_belatedPNG.js"></script>'."\n";
    		
    $html .= '<!--[if lte IE 6]>'."\n";
    $html .= '<link rel="stylesheet" rel="stylesheet" type="text/css" media="all" href="'.LINK_CSS.'menu_ie2.css" />'."\n";
    		
    $html .= '<script src="'.LINK_JS.'DD_belatedPNG.js"></script>'."\n";
    $html .= '<script>'."\n";
    $html .= 'DD_belatedPNG.fix(\'#squareImage\');'."\n";
    $html .= '</script>'."\n";
    $html .= '<![endif]-->'."\n";
    $html .= '</head>'."\n";
    		
    $html .= ''."\n";
    
    return $html;
    }
    
    // render the snippet to the page
    echo ( RenderHead() );
    ?>



    Page Template:
    [[InsidePage]]
    
      • 4172
      • 5,888 Posts
      Don’t know what are the differences between your templates, but this is what chunks are for. If you have html, which is the same on several pages or templates create for example a chunk ’head’ with something like:

      <head>
      
      <title></title>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
      <meta name="keywords" content="" />
      <meta name="description" content="" />
      		
      <link rel="stylesheet" type="text/css" media="all" href="[*LINK_CSS*]style.css" />
      <link rel="stylesheet" type="text/css" media="print" href="[*LINK_CSS*]print.css" />
      
      <link rel="shortcut icon" href="[*IMAGES*]favicon.ico" />
      		
      <script language="javascript" type="text/javascript" src="[*LINK_JS*]menu.js"></script>
      <script language="javascript" type="text/javascript" src="[*LINK_JS*]DD_belatedPNG.js"></script>
      		
      <!--[if lte IE 6]>
      <link rel="stylesheet" rel="stylesheet" type="text/css" media="all" href="[*LINK_CSS*]menu_ie2.css" />
      		
      <script src="[*LINK_JS*]DD_belatedPNG.js"></script>
      <script>
      DD_belatedPNG.fix(\'#squareImage\')
      </script>
      <![endif]-->
      </head>	
      
      


      and for LINK_CSS and so on create TVs perhaps dropdown-Tvs.

      In your Templates, (if you still need more than one) you need only to put your chunks and other TVs like that:

      
      <!DOCTYPE HTML .....................................................................>
      <html>
      
      {{head}}
      <body>
      [*perhaps a navigation-TV with different wayfinder calls*]
      [*content*]
      [*sidebarTV*] or {{sidebarchunk}}
      {{footer}}
      </body>
      </html>
      


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

        you can buy me a beer, if you like MIGX

        http://webcmsolutions.de/migx.html

        Thanks!
        • 7914
        • 137 Posts
        (shoot, you already beat me to a reply)
        I wanted to give more info to what I am trying to do. I completely forgot about chunks. I guess what I’m doing is making php generate my chunks??

        Here is what my default template looks like now:
        [[OpenInsidePage]]
        
        <ul id="sideNav">
        <li><a href="#">Side Nav</a></li>
        </ul>
        
        [[OpenPageContent]]
        
        <h1>[*#pagetitle*]</h1>
        [*#content*]
        
        [[ClosePageContent]]
        
        [[CloseInsidePage]]
        


        Everything is working great. I am able to update the content etc. I am just not able to update the metadata for some reason. Guess I’ll keep digging. Thank you for your suggestion, I appreciate it!
          • 3749
          • 24,544 Posts
          I doubt if it’s related, but I wouldn’t use $html as a variable name. wink
            Did I help you? Buy me a beer
            Get my Book: MODX:The Official Guide
            MODX info for everyone: http://bobsguides.com/modx.html
            My MODX Extras
            Bob's Guides is now hosted at A2 MODX Hosting
            • 7914
            • 137 Posts
            Do you think there is a difference/prefrence in method? Using snippets to generate the page or using chunks? -- Other than the obvious 1 is php, the other html smiley
              • 28042 ☆ A M B ☆
              • 24,524 Posts
              You can’t call a snippet with MODx tags in snippet code; PHP doesn’t understand the MODx templating syntax! You have to use $modx->runSnippet(’snippetName’, array(’param1’->’value1’, ...)); in order to run one snippet from inside of another snippet.
                Studying MODX in the desert - http://sottwell.com
                Tips and Tricks from the MODX Forums and Slack Channels - http://modxcookbook.com
                Join the Slack Community - http://modx.org
                • 7914
                • 137 Posts
                @sottwell Thank you! that will come in handy down the road I just know it.

                Thank you everyone for your help, I really appreciate it. I am excited to get to the point where I can start helping others and not ask all the questions. smiley