Master Snippet to control other Dynamic Snippets

126 views Asked by At

I am working on a project that allows for a massive import of data into a ModX website.

With this, I am needing to be able to display the data from the fields imported, but need to be able to allow the client to place each individual field where ever they would like it to be displayed in a content resource.

So, my thoughts were to create individual snippets that would only pull individual field each.

However, I know that this would be a complete hog on the system having to query the table umpteen numbers of times, as there are potentially hundreds of fields.

How can I create a "master snippet" that will pull all fields, but I can still allow them to be used like snippets?

For instance... one pull to grab Rec1Field1, Rec1Field2, Rec1Field3, Rec1Field4, but be able to use them in a content resource like [[Rec1Field1]], [[Rec1Field2]], [[Rec1Field3]], [[Rec1Field4]]

2

There are 2 answers

1
Kristian Sandström On BEST ANSWER

Another alternative is using $modx->setPlaceholders(array('placeholder'=>'value', ...)); in your snippet, which will generate placeholders accessible anywhere in your template or nested chunks by a simple [[+placeholder]]. This would allow the user to place the tag in the content rich text or wherever he might want it, without you having to do string parsings or bother with complicated chunks.

3
OptimusCrime On

You can do another approach on this.

In Modx you have a function called $modx->getChunk that allows you to take a chunk with placeholders and supply an array with keys that match the placeholders. These placeholders will then be replaced by the value of the keys.

This is obviously not what you want, but there already exists a working example of how to use a string with parsing placeholders.

See this tutorial

So, what you do is this:

  1. Allow your customers to place placeholders in the content-field where they want.
  2. In the template where you are to display the content, call a custom snippet, like:

    [[parse_content_with_placeholders? &content=`[[*content]]` ]]

In this snippet you do something like:

// Properties
$props = array('Rec1Field1' => 'Foo','Rec1Field2' => 'Bar');

// Create the temporary chunk
$uniqid = uniqid();
$chunk = $modx->newObject('modChunk', array('name' => "{tmp}-{$uniqid}"));
$chunk->setCacheable(false);

$output = $chunk->process($props, $content);