Creating MODX Articles in Code III

Handling placeholders when creating Articles with PHP


In the previous article, we looked at how to use a chunk to set the content of Articles resources created in code. In this one, we'll we'll look at how to set the content of your Article resource using a chunk and replace selected placeholders in the chunk during the process.


MODX logo

Replacing Placeholders Immediately

As we saw in the previous article, MODX will put placeholders in the chunk into the content field of the Article resource unchanged. They'll be parsed and replaced when MODX renders the Article resource for a visitor's browser.

There may be cases where you want to replace some or all of the placeholders immediately. For example, you might be creating user pages that already have some standard content to get the user started. You might want to put the user's name and email into the page. You already know the values for those placeholders and you might as well put them into the page as is. Since your chunk will be used for multiple users, you'll want to use placeholders for those. Here's a simplified example such a chunk:

<br>
<h3>[[+user_name]]'s Page</h3>
<p class="user_email">[[+user_email]]</p>

/* Article Creation Code */

/* Set this to the ID of the Articles Container resource  */
$parent = 12;

/* Set this to the ID of an Article template
    (*not* the Article container template) */
$template = 122; set to the ID of the Articles Template.

/* Set this to the ID of the page creator */
$creator = 1;

$pagetitle = 'Some Pagetitle';
$alias = 'some-pagetitle';

$placeholders = array(
    'user_name' => $userName,
    'user_email => $userEmail,
);


$doc = $modx->newObject('modResource');
$doc->set('class_key', 'Article');
$doc->set('parent', $parent);
$doc->set('pagetitle', $pagetitle);
$doc->set('template', $template);
$doc->set('alias', $alias);
$doc->set('createdon', time());
$doc->set('published', $published);

/* Set publishedon field if published */
if ($published) {
    $doc->set('publishedon', time());
}

$doc->setContent($modx->getChunk('someChunk', $fields));

$doc->save();

We've sent the $fields array as the second argument to $modx->getChunk(). When MODX retrieves the chunk it will look for placeholders that match the keys of the array and replace them with their values.


Other Placeholders

What if there are other placeholders that you don't want to replace? You may have some other tags in the array that you want to put into the content field unchanged. For example, they might be placeholders that will be replaced by getResources or Wayfinder when the page is rendered. The good news is that if they don't match anything in the $fields array, MODX will leave them alone.

Here's a really simple example. Suppose the chunk contains this code:

<p>[[+p1]]</p>
<p>[[+p2]]</p>
<p>[[+p3]]</p>

Your code to create the resource looks like this:

/* Article Creation Code */
$parent = 12; // set this to the ID of an Articles Container
$pagetitle = 'Some Pagetitle';

$fields = array(
    'p1' => 'This is p1',
    'p3' => 'This is p3',
);

$doc = $modx->newObject('modResource');
$doc->set('class_key', 'Article');
$doc->set('parent', $parent);
$doc->set('pagetitle', $pagetitle);

/* More fields set here
    ...
*/

$doc->setContent($modx->getChunk('someChunk', $fields));
$doc->save();

The final content of the Article resource you create will look like this:

<p>This is p1</p>
<p>[[+p2]]</p>
<p>This is p3</p>

The p2 placeholder is unchanged because it's not in the $fields array.


Coming Up

In the next article, we'll look at how to use a MODX processor to create your Article resource, and discuss why that's a much better method to use.



For more information on how to use MODX to create a web site, see my web site Bob's Guides, or better yet, buy my book: MODX: The Official Guide.

Looking for high-quality, MODX-friendly hosting? As of May 2016, Bob's Guides is hosted at A2 hosting. (More information in the box below.)



Comments (0)


Please login to comment.

  (Login)