Coded something up in Couch in an interesting way? Have a snippet or shortcode to share? Post it here for the community to benefit.
17 posts Page 2 of 2
I have added @snippets and @uploads keywords that are recognized and fixed to actual paths on server (be it custom-defined or CMS-default). I'll use it to automatically create empty snippets files to bootstrap a new template, if those do not exist yet. Updated code in public repository.
Is there a way to output couchcms code using <cms:write>

For example:
Code: Select all
<?php require_once( '../couch/cms.php' ); ?>
<cms:write "test.php" >
   <cms:template title="Dynamic Form Generation" clonable="1" >
      <cms:pages masterpage="test/index.php">
         <cms:show_repeatable "form_fields">
            <cms:editable name="<cms:show input_name />" label="<cms:show input_title />" type="<cms:show input_type />" order="<cms:show k_absolute_count />" />
         </cms:show_repeatable>
      </cms:pages>
   </cms:template>
</cms:write>
<?php COUCH::invoke(); ?>


Should output in the "test.php" file (as content):
Code: Select all
   <cms:template title="Dynamic Form Generation" clonable="1" >
      <cms:pages masterpage="test/index.php">
         <cms:show_repeatable "form_fields">
            <cms:editable name="<cms:show input_name />" label="<cms:show input_title />" type="<cms:show input_type />" order="<cms:show k_absolute_count />" />
         </cms:show_repeatable>
      </cms:pages>
   </cms:template>


Regards,
GXCPL (CTR)
Image
where innovation meets technology
This works for me —
Code: Select all
<cms:capture into='dynatext'>
    <%cms:if <cms:show name /> eq '1'>Yes<%cms:else/>No</%cms:if>
</cms:capture>

<cms:write 'test.txt'><cms:swap val='%cms:' with='cms:' text=dynatext /></cms:write>


I hope you get the idea which tags are executed and which are not. (Tag cms:swap is my custom tag for replacing substrings).
trendoman wrote: This works for me —
Code: Select all
<cms:capture into='dynatext'>
    <%cms:if <cms:show name /> eq '1'>Yes<%cms:else/>No</%cms:if>
</cms:capture>

<cms:write 'test.txt'><cms:swap val='%cms:' with='cms:' text=dynatext /></cms:write>


I hope you get the idea which tags are executed and which are not. (Tag cms:swap is my custom tag for replacing substrings).


@trendoman,

Thanks for the heads-up. This did help me. Though I didnt create a tag, but I directly used the str_replace() of php to achieve what was intended. Also, rather than replacing '%cms:' to 'cms:' I just replaced the '%' and it was done.

Regards,
GXCPL (CTR)
Image
where innovation meets technology
I found the need to write the files in a more organized manner and decided I want a main folder and then nested folders with the user id of whoever is creating the file.

The expected use of the tag's new parameter would be:

Code: Select all
<cms:set file_name = "<cms:url_friendly name=k_page_title separator='_' extension='php' />" />
<cms:write file_name folder="tb/<cms:show k_user_id />" truncate="1">....</cms:write />


And therefore the updated addon would be:

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

    extract( $FUNCS->get_named_vars(
                array(
                      'file'=>'',       /* file name if provided needs to be relative to the site directory */
                      'folder'=>'',     /* folder location relative to site directory */
                      'truncate'=>'0',  /* will begin afresh */
                      'add_newline'=>'0',   /* appends newline character to the content */
                    ),
                $params)
           );

    // sanitize params
    $file = trim( $file );
    if( !$file ){
        $file = 'my_log.txt';
    }
   
    $folder = trim($folder, '/');
    $folder = ($folder != '') ? rtrim($folder, '/') . '/' : '';

    // Combine folder, site directory, and file name
    $fullPath = K_SITE_DIR . $folder . $file;

    // Create directories if they do not exist
    $directory = dirname($fullPath);
    if (!is_dir($directory)) {
        //Specify the permissions for the newly created directories
        mkdir($directory, 0777, true);
    }

    $truncate = ( $truncate == 1 ) ? 1 : 0;
    $add_newline = ( $add_newline == 1 ) ? 1 : 0;

    $content = '';
    foreach( $node->children as $child ){
        $content .= $child->get_HTML();
    }
    if( $add_newline ){
        $content .= "\r\n";
    }

    $fp = @fopen( $fullPath, 'a' );
    if( $fp ){
        @flock( $fp, LOCK_EX );
        if( $truncate ){
            ftruncate( $fp, 0 );
            rewind( $fp );
        }
        @fwrite( $fp, $content );
        @flock( $fp, LOCK_UN );
        @fclose( $fp );
    }

    return;
}


This will look if the specified folders already exist and it not, they will be created.

Thought maybe others would benefit from this small addition.
@aleksandru, it seems the default tag can handle folders like this —
Code: Select all
<cms:write file="<cms:concat  'tb/'  k_user_id  '/'  file_name  />" truncate="1">....</cms:write />
trendoman wrote: @aleksandru, it seems the default tag can handle folders like this —
Code: Select all
<cms:write file="<cms:concat  'tb/'  k_user_id  '/'  file_name  />" truncate="1">....</cms:write />


Hi @trendoman,

That is true, I just find it easier and cleaner having it as a parameter rather than using concatenation :)
Just a personal preference :D
17 posts Page 2 of 2