We launched new forums in March 2019—join us there. In a hurry for help with your website? Get Help Now!
  • Here is a snippet I authored at Etomite about a year ago, for my first large project, to emulate something like blocks in the nuke world. Not really necessary now with some of the alternative TV approaches to the same result (especially since this requires you to assign aliases to all of your documents), but, thought I’d share the code. Anyway, I updated a few things, added some comments and the code follows the example below. Just copy and paste the snippet code into a new snippet called AliasContent and call it in your templates/chunks/TVs like so:

    <div id="left-column">
    [[AliasContent?contentName=left-column]]
    [[AliasContent?contentName=left-column-footer&mode=2]]
    </div>

    This example would look for chunks and snippets named left-content or [*alias*]-left-content and output them, then look for chunks and snippets named [*alias*]-left-column-footer and prepend those (&mode=2) to any named simply left-column-footer.

    //AliasContent v0.3
    //by OpenGeek, [email protected]
    //Provides a means of providing specific content for 
    //  specific areas of a template and allowing each
    //  area to append, prepend, or override the core content
    //  with alias-specific content.  Core content is defined
    //  as any Snippet and/or Chunk, and alias-specific content is
    //  defined as Snippets and/or Chunks of the same name prefixed
    //  with "`alias`-"
    // (e.g. [[AliasContent? &content=`LeftColumn`]] looks for a chunk and/or snippet named 
    //  home-LeftColumn when loaded on a document with the alias of 'home')
    // NOTE: Chunks with the same name as a snippet are output before the corresponding snippet is executed.
    // NOTE: All documents that use this would require an alias, otherwise only default content would be loaded.
    $snippetOutput = '';
    $coreContent = '';
    $aliasContent = '';
    $docAlias = $modx->documentObject['alias'];
    
    //$contentName: name of the snippet or chunk to output
    //$mode: override (default, 0), append (1), or prepend (2)
    //  with alias-specific content
    $mode = isset($mode)?$mode:0;
    if (isset($contentName))
    {
    	
    	$aliasContent = $modx->putChunk("$docAlias-$contentName");
    	$coreContent = $modx->putChunk("$contentName");
    	
    	
    	if ( $mode==1 )
    	{
    		//Append alias specific content after default/core content
    		$snippetOutput = $coreContent;
    		$snippetOutput .= ($out= $modx->runSnippet("$contentName"))? $out: '';
    		$snippetOutput .= $aliasContent;
    		$snippetOutput .= ($out= $modx->runSnippet("$docAlias-$contentName"))? $out: '';
    	}
    	if ( $mode==2 )
    	{
    		//Prepend alias specific content before default/core content
    		$snippetOutput = $aliasContent;
    		$snippetOutput .= ($out= $modx->runSnippet("$docAlias-$contentName"))? $out: '';
    		$snippetOutput .= $coreContent;
    		$snippetOutput .= ($out= $modx->runSnippet("$contentName"))? $out: '';
    	}
    	if ( $mode==0 )
    	{
    		// Override default/core content with alias-specific content
    		if ( strlen($aliasContent) > 0 )
    		{
    			$snippetOutput = $aliasContent;
    			$snippetOutput .= ($out= $modx->runSnippet("$docAlias-$contentName"))? $out: '';
    		}
    		else
    		{
    			$aliasSnippet = $modx->runSnippet("$docAlias-$contentName");
    			if ( $aliasSnippet === false )
    			{
    				$snippetOutput = $coreContent;
    				$snippetOutput .= ($out= $modx->runSnippet("$contentName"))? $out: '';
    			}
    			else
    			{
    				$snippetOutput = $aliasSnippet;
    			}
    		}
    	}
    }
    
    return $snippetOutput;
    
      • 6726
      • 7,075 Posts
      Thanks for sharing : even if, as you say, there are more flexible alternate way to handle this, some users might want to do it this way.

      It’s always nice to have alternatives, and one thing I very much like with MODx : you always have more than one way to achieve a particular goal...
        .: COO - Commerce Guys - Community Driven Innovation :.


        MODx est l&#39;outil id