Problems, need help? Have a tip or advice? Post it here.
5 posts Page 1 of 1
Hello.
I was trying to build a shortcode that would replace everything in between the brackets with some specific words.

Here's what I did :

Code: Select all
$FUNCS->register_tag( 'replace_variables', replace_variables_handler );
function replace_variables_handler( $params, $node ){
    global $FUNCS;

    // call the children
    foreach( $node->children as $child ){
        $html .= $child->get_HTML();
    }

    $html = str_replace("[name]", "<cms:pages masterpage="contrats.php" custom_field='my_uid=notification_contrat_relie' ><cms:show workers_name /></cms:pages>", $html );
    $html = str_replace("[phone]", "<cms:pages masterpage="contrats.php" custom_field='my_uid=notification_contrat_relie' ><cms:show workers_phone /></cms:pages>", $html );
    return $html;
}


This shortcode would only be displayed and used on the "contrat-conf.php" page, which has the following code:

Code: Select all
<cms:pages masterpage="notifications/contrat-conf.php" custom_field="date_envoi_contrat_conf=<cms:date format='Y-m-d H:i:00' />">
<cms:send_mail from='admin@mysite.com' to="<cms:show_repeatable 'destinataires'><cms:show destinataire_simple/></cms:show_repeatable>" subject='Subject' debug="1">
<cms:related_pages 'notification_contenu' >[replace_variables]<cms:show gabarit_content/>[/replace_variables]</cms:related_pages>
</cms:send_mail>
</cms:pages>


See where the [replace_variables] shortcode is?

I need to make sure it scans the "gabarit_content" before it sends the email, and replaces every occurence of [name] by the name matching that page ID in the "contrats.php" page. Same for [phone].

The "gabarit_content" region currently echoes the following text:

Code: Select all
Hello [name]!
Here's your new phone number : [phone]
Enjoy!


Obviously, I need to replace [name] with the name of the person entered in the "contrats.php" page, with a matching ID, and so on.

Any idea how to make this work?

I would create different shortcodes for different pages if needed.

Thanks a lot
Hi,

As explained in the docs (http://docs.couchcms.com/miscellaneous/shortcodes.html), we need to make use of <cms:do_shortcodes> to process the shortcodes. In your case, we could use it like this -
Code: Select all
<cms:send_mail..>
    <cms:do_shortcodes>
        <cms:related_pages 'notification_contenu' >[replace_variables]<cms:show gabarit_content/>[/replace_variables]</cms:related_pages>
    </cms:do_shortcodes>
</cms:send_mail>

That said, there is another problem in your code. You are trying to use Couch tags from within a PHP function (your shortcode handler) -
$html = str_replace("[name]", "<cms:pages masterpage="contrats.php" custom_field='my_uid=notification_contrat_relie' ><cms:show workers_name /></cms:pages>", $html );
$html = str_replace("[phone]", "<cms:pages masterpage="contrats.php" custom_field='my_uid=notification_contrat_relie' ><cms:show workers_phone /></cms:pages>", $html )
;
PHP wouldn't know what to do with it and will try to replace [name] with the literal string "<cms:pages .. ".

Please see "Obfuscate email" shortcode example in the docs page mentioned above on how to "execute" Couch tags first to get their output. This then can be passed on to str_replace().

Hope this helps.
Thanks KK.

Still not working though.

I took the "mail" shortcode, and replaced stuff in it:

Code: Select all
   $FUNCS->register_shortcode( 'replace_variables', 'replace_handler' );
   function replace_handler( $params, $content=null ){
      global $FUNCS;

      $html = str_replace("[name]", "John", $html );

      // Pass on the code to Couch for execution using the 'embed' function
      return $FUNCS->embed( $html, $is_code=1 );
   }


I did a test, with replacing Couch tags with a regular string, and it's not working.
Here's the code on the actual page:

Code: Select all
<cms:pages masterpage="notifications/contrat-conf.php" custom_field="date_envoi_contrat_conf=<cms:date format='Y-m-d H:i:00' />">
<cms:send_mail from='admin@mysite.com' to="<cms:show_repeatable 'destinataires'><cms:show destinataire_simple/></cms:show_repeatable>" subject='Subject' debug="1">
<cms:do_shortcodes>
<cms:related_pages 'notification_contenu' >[replace_variables]<cms:show gabarit_content/>[/replace_variables]</cms:related_pages>
</cms:do_shortcodes>
</cms:send_mail>
</cms:pages>


Any idea why? It's just not showing anything on the page, not even the content of the "gabarit_content" region.
Thanks!
There is a mistake in your handler code. Please use the following ($html replaced with $content) -
Code: Select all
$FUNCS->register_shortcode( 'replace_variables', 'replace_handler' );
   function replace_handler( $params, $content=null ){
      global $FUNCS;

      $html = str_replace("[name]", "John", $content );

      // Pass on the code to Couch for execution using the 'embed' function
      return $FUNCS->embed( $html, $is_code=1 );
   }

This is definitely working as I just checked it.
Nice! It's working.

Now I just need to find a way to make multiple "str_replace" lines.
I tried this:

Code: Select all
 $html = str_replace("[name]", "<cms:pages masterpage="contrats.php" custom_field='my_uid=notification_contrat_relie' ><cms:show workers_name /></cms:pages>", $html );
    $html = str_replace("[phone]", "<cms:pages masterpage="contrats.php" custom_field='my_uid=notification_contrat_relie' ><cms:show workers_phone /></cms:pages>", $html );


But it's only working for the phone text. I guess that it always overwrite the last str_replace text.
Any idea how to do this? Thanks!

-----

EDIT :

Nevermind, found out how to handle this with arrays. I did this and it worked perfectly :

Code: Select all
$FUNCS->register_shortcode( 'replace_variables', 'replace_handler' );
   function replace_handler( $params, $content=null ){
      global $FUNCS;

      $search = array("[workers_name]", "[workers_phone]");
      $replace = array("Paul", "555-555-5555");

      $html = str_replace($search, $replace, $content );

      // Pass on the code to Couch for execution using the 'embed' function
      return $FUNCS->embed( $html, $is_code=1 );
   }

Thanks a lot for your help KK!
5 posts Page 1 of 1
cron