Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
4 posts Page 1 of 1
I'm finding this quite useful:
<cms:set example = 'CouchCMS is great' />

<cms:show example case = 'upper' /> will display "COUCHCMS IS GREAT"

<cms:show example case = 'lower' /> will display "couchcms is great"

Here's the modified function:

Code: Select all
        function show( $params, $node ){
            global $FUNCS, $CTX;
            if( count($node->children) ) {die("ERROR: Tag \"".$node->name."\" is a self closing tag");}

            extract( $FUNCS->get_named_vars(
                        array( 'var'=>'', /*placeholder*/
                               'scope'=>'',
                               'as_json'=>'0',
                        'case'=>'',
                              ),
                        $params)
                   );

            $value = $params[0]['rhs'];
            $scope = strtolower( trim($scope) );
            $as_json = ( $as_json==1 ) ? 1 : 0;
         $case = strtolower( trim($case) );

            // If scope set and first param is a variable, return variable only from the specified scope scope.
            if( $scope != '' && $node->attributes[0]['value_type']==K_VAL_TYPE_VARIABLE ){
                if( $scope!='1' && $scope!='2' && $scope!='global' && $scope!='local'  ){
                    die("ERROR: Tag \"".$node->name."\" has unknown scope '" . $scope. "'. Only 'global (2)' or 'local (1)' are valid.");
                }
                $scope = ( $scope=='global' || $scope=='2' ) ? 2 : 1;

                $value = $CTX->get( $node->attributes[0]['value'], $scope );
            }
         
         if( $case == 'upper' ){ $value = strtoupper( $value );}
         if( $case == 'lower' ){ $value = strtolower( $value );}

            if( $as_json && is_array($value) ){ $value = $FUNCS->json_encode( $value ); }
            return $value;
        }
@dmore54, it is a valuable contribution) nice!

Note that there will be problems with languages and once Couch is updated the change also will be lost. Let me suggest a version that can be copied to couch/addons/kfunctions.php instead and deals with language problems. I have also provided a way to add more case scenarious such as Title.

Using a listener of the event provided by CouchCMS we can let <cms:show> do its job first (taking care of all intricacies of inner tags, arrays, json formatting etc), then use its resulting output to apply the desired case afterwards.

UPDATED.

Code: Select all
<?php

   /**
   *   Add 'case', 'encoding' params to <cms:show> tag
   *
   *   @example  <cms:show case='upper' />
   *   @example  <cms:show case='title' />
   *   @example  <cms:show case='lower' encoding='Windows-1251' />
   *   @author @dmore54, @trendoman <tony.smirnov@gmail.com>
   *   @date   20.05.2021
   *   @last   30.06.2022
   */

   $FUNCS->add_event_listener( 'tag_show_executed', function ($tag_name, $params, $node, &$html) {
      if( !function_exists('mb_convert_case') ) return;

      $case = '';
      $encoding = 'UTF-8';

      for( $x=0; $x<count($params); $x++ ){
          $attr = strtolower(trim($params[$x]['lhs']));
          if( $attr=='case' ){
              $case = strtolower(trim($params[$x]['rhs']));
              continue;
          }
          if( $attr=='encoding' ){
              $encoding = trim( $params[$x]['rhs'] );
              continue;
          }
      }

      switch ($case) {
         case "u":
         case "upper":
           $type = MB_CASE_UPPER;
           break;
         case "l":
         case "lower":
           $type = MB_CASE_LOWER;
           break;
         case "t":
         case "title":
           $type = MB_CASE_TITLE;
           break;
         default:
           return;
      }

      $html = mb_convert_case($html, $type, $encoding);
   });

   /*
   // ~~~~~~~~~~~~~
   // Credits
   // ~~~~~~~~~~~~~
   // You should have downloaded this code from https://github.com/trendoman/Tweakus-Dilectus
   // ~~~~~~~~~~~~~
   // Support
   // ~~~~~~~~~~~~~
   // Write me at <anton.cms@ya.ru>, <tony.smirnov@gmail.com> "Anton S aka Trendoman"
   // Telegram: https://t.me/couchcms
   */
Fantastic, thanks!
This mod is on GitHub —

Tweakus-Dilectus Modded Tags » show
4 posts Page 1 of 1