How to escape symbols like apostrophes

I have my meta title tag set up like this:


 <meta property='og:title' content="[[*id:ne=`[[++site_start]]`:then=`[[*MetaTitle:ne=``:then=`[[*MetaTitle]]`:else=`[[*pagetitle:striptags]]`]]`]]">

This is showing errors when generating anyting with symbols in the title. Namely an apostrophie.

How would I escape these from here? thanks

When you say it’s showing errors, what do you mean by that? Do you have an example of your errors?

Also, could you provide an example of one of the troublesome pagetitles?

As an aside, when you get into this kind of nesting for logic in your templates, where there is a lot of recursion, it’s often better to write a small snippet to perform the logic. People here might be willing to help you craft that snippet.

Here is an example:

So the main issue is that if you look at the og:title meta property, the apostrophe following the word “fixin” causes the property content to end early and my ADA compliance checker is finding an error in the attribute because of this.

And i’m noticing now here too, that the title is getting chopped off after “fixin” becasue of that.

I think you’re just too deeply nested for the conditional parser, especially when the intermediate tags contain apostrophes.

Here’s my guess at the snippet you need (untested), though I’m not sure I follow the logic of your conditional modifiers.

<meta property='og:title' content="[[!MyTitle]]">

/* MyTitle snippet */
$doc = $modx->resource;
$id = $doc->get('id');

$title = '';

if ($id != $modx->getOption('site_start') {
      $metaTitle = $doc->getTVValue('MetaTitle');
      if (! empty($metaTitle)) {
         $title = $metaTitle;
      } 
}

return empty($title)
    ?  strip_tags($doc->get('pagetitle')) 
     : $title;

FWIW, unless your site start page changes often, it’s pretty inefficient to test for it on every page of the site. I’d suggest just putting whatever you want directly in that tag on the site_start page (no snippet tag, no modifiers).

Then, you can just do this in the snippet (much simpler and faster):

$doc = $modx->resource;
$metaTitle =  $doc->getTVValue('MetaTitle');
if (! empty($metaTitle)) {
   $title = $metaTitle;
} else {
    $title = strip_tags($doc->get('pagetitle'));
} 
return $title;
1 Like

The primary thing to do, is to run htmlentities on your output. That encodes any special html character so it doesn’t break your markup.

(And also protects against cross site scripting, tho in this case I’d say the page titles are more or less trusted input.)

MODX has a htmlent output modifier to help you with that. For example:

 <meta 
    property='og:title' 
    content="[[*id:ne=`[[++site_start]]`:then=`[[*MetaTitle:ne=``:then=`[[*MetaTitle]]`:else=`[[*pagetitle:striptags]]`]]`:htmlent]]">

I’d probably simplify that tag to this:

 <meta 
    property="og:title"
    content="[[*MetaTitle:default=`[[*pagetitle]]`:striptags:htmlent]]">

If you leave the MetaTitle TV empty on your homepage, that has the same output but without the extra conditional.

1 Like

Mark you saved my life! Well, maybe my sanity at least!

Thank you for that! I never knew what the htmlent conditional was for!

1 Like

This topic was automatically closed 2 days after the last reply. New replies are no longer allowed.