Problems, need help? Have a tip or advice? Post it here.
15 posts Page 1 of 2
hello - a short shortcode question ... can a Couch variable such as k_admin_link be included in the output of a shortcode? Nothing I've tried so far has worked!
Hi Potato,

All Couch variables are accessible through PHP (via the $CTX object as illustrated below)
Code: Select all
$FUNCS->register_shortcode( 'test', 'test_handler' );

function test_handler( $params, $content=null ){
   global $CTX;

   $link = $CTX->get('k_admin_link');

   return $link;
}

Using [test] should output the admin-link.

Of course this is a contrived example. A real life tag would be much more flexible but I think this example illustrates the point.

Hope this helps.
hi - just getting back to this shortcode and trying to retrieve k_page_id but unfortunately not succeeding.

My shortcode looks like this:
Code: Select all
<?php
$FUNCS->register_shortcode( 'modal', 'modal_handler' );
function modal_handler( $params, $content=null ){
global $FUNCS, $CTX;
extract( $FUNCS->get_named_vars(array(
'page' => $CTX->get( 'k_page_id' ),
'name' => '#'
), $params) );
$html = " *** all the markup for the modal in here ***";
return $FUNCS->embed( $html, $is_code=1 );
}

In order to keep things clearer I have removed a large amount of markup and replaced it with *** all the markup for the modal in here ***

I am successfully outputting:

<cms:get_custom_field 'list_item' masterpage='list-items.php' page='{$name}' />

by the shortcode usage: [modal name="abc-xyz"]

I've tried {$page} to get k_page_id but nothing happens. I've looked at the Paypal example in the docs but almost everything else I've tried results in a nasty outpouring of php errors.

Help gratefully received!
Hi potato,

The part where you generate the code that gets passed on to $FUNCS->embed is missing so I cannot make out how you propose to use the page_id.

Please post or PM me the full code.

Also please take some time to explain what exactly your code is supposed to be doing. What is the ideal use-case - I mean what the user should input and what should be the expected output?

Thanks.
Yes, sorry, I am aiming to allow a client to inject modal windows into any richtext editable area. I will create a cloneable template say, modals.php, with probably 3 editable areas 1. modal launch button text 2. modal content heading and 2. modal content. The client will add a new modals.php cloned page and fill in the data. They will take the cloned page name they have assigned and enter the following within the richtext editable area of the other page:

[modal name="xyz"]

I have no problem getting hold of the data from a cloned page within the shortcode (I'm using a cloned template called list-items.php just for now - testing it all out on my local machine).

I want to allow the client to inject more than one modal window per richtext editable area - so I need to assign a unique identifier to various parts of the modal markup (from Bootstrap) - and this is where k_page_id comes in.

I hope this all makes sense.


this is the full shortcode:
Code: Select all
<?php
$FUNCS->register_shortcode( 'modal', 'modal_handler' );
function modal_handler( $params, $content=null ){
global $FUNCS, $CTX;
extract( $FUNCS->get_named_vars(array(
'page' => $CTX->get( 'k_page_id' ),
'name' => '#'
), $params) );
$html = "<button class=\"btn btn-primary btn-lg\" data-toggle=\"modal\" data-target=\"#myModal\">
           Launch demo modal {$page}
         </button>
         <div class=\"modal fade\" id=\"myModal\" tabindex=\"-1\" role=\"dialog\" aria-labelledby=\"myModalLabel\" aria-hidden=\"true\">
           <div class=\"modal-dialog\">
             <div class=\"modal-content\">
               <div class=\"modal-header\">
                 <button type=\"button\" class=\"close\" data-dismiss=\"modal\"><span aria-hidden=\"true\">&times;</span><span class=\"sr-only\">Close</span></button>
                 <h4 class=\"modal-title\" id=\"myModalLabel\">Modal title</h4>
               </div>
               <div class=\"modal-body\">
               <cms:get_custom_field 'list_item' masterpage='list-items.php' page='{$name}' />
               </div>
               <div class=\"modal-footer\">
                 <button type=\"button\" class=\"btn btn-default\" data-dismiss=\"modal\">Close</button>
               </div>
             </div>
           </div>
         </div>";
return $FUNCS->embed( $html, $is_code=1 );
}


It is a bit experimental at the moment ...
Got it now :)

Well, I think the 'page id' parameter is redundant as we already know the page's name as identifier.
We can use the page-name to fetch the page into context and then we'll have access to all its data (including the page_id and the custom-fields).

Please try the following modified version of your code -
Code: Select all
$FUNCS->register_shortcode( 'modal', 'modal_handler' );
function modal_handler( $params, $content=null ){
    global $FUNCS, $CTX;
    extract( $FUNCS->get_named_vars(array(
        'name' => '#'
    ), $params) );
   
    $html =<<<EOS
    <cms:pages masterpage='list-items.php' page_name='$name' limit='1'>
        <button class="btn btn-primary btn-lg" data-toggle="modal" data-target="#myModal">
           Launch demo modal <cms:show k_page_id />
         </button>
         <div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
           <div class="modal-dialog">
             <div class="modal-content">
               <div class="modal-header">
                 <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                 <h4 class="modal-title" id="myModalLabel">Modal title</h4>
               </div>
               <div class="modal-body">
                <cms:show list_item />
               </div>
               <div class="modal-footer">
                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
               </div>
             </div>
           </div>
         </div>
     </cms:pages>
EOS;

    return $FUNCS->embed( $html, $is_code=1 );
}

As you can see, we make use of the 'name' parameter to make cms:pages tag fetch the right page. Rest is as per usual.

Hope this helps.
thanks so much KK - very elegant ... I'll test it all out tomorrow and when it's working I'll post it on the Tips & Tricks as other people may find it useful.
... back on the shortcode for modal windows ... I wanted to allow a modal window to be triggered from either a button OR some inline text. For this to work I need to separate out the modal window trigger and the modal window itself. To achieve this I have used cms:capture within the shortcode as follows:
Code: Select all
$FUNCS->register_shortcode( 'modal', 'modal_handler' );
function modal_handler( $params, $content=null ){
    global $FUNCS, $CTX;
    extract( $FUNCS->get_named_vars(array(
        'name' => '#'
    ), $params) );
   
    $html =<<<EOS
    <cms:pages masterpage='modal.php' page_name='$name' limit='1'>
       <cms:if modal_button eq 'yes' >   
           <button class="btn btn-primary" data-toggle="modal" data-target="#<cms:show k_page_name />">
              <cms:show modal_launch_text />
            </button>              
      <cms:else />
         <a class="launch-modal" data-toggle="modal" data-target="#<cms:show k_page_name />">
              <cms:show modal_launch_text />
            </a>   
      
      </cms:if>
      <cms:set show_buffer='0' 'global' />
      <cms:capture into='my_buffer'>
         <div class="modal fade" id="<cms:show k_page_name />" tabindex="-1" role="dialog" aria-labelledby="<cms:show k_page_name />Label" aria-hidden="true">
           <div class="modal-dialog<cms:if modal_size eq 'wide' > modal-lg</cms:if><cms:if modal_size eq 'narrow' > modal-sm</cms:if>">
             <div class="modal-content">
               <div class="modal-header">
                 <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                 <h4 class="modal-title" id="<cms:show k_page_name />Label"><cms:show modal_heading /></h4>
               </div>
               <div class="modal-body">
                <cms:show modal_content />
               </div>
               <div class="modal-footer">
                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
               </div>
             </div>
           </div>
         </div> 
         <cms:set show_buffer='1' 'global' />
      </cms:capture>
     </cms:pages>
EOS;

    return $FUNCS->embed( $html, $is_code=1 );
}   


along with
Code: Select all
<cms:if show_buffer ><cms:show my_buffer /></cms:if>
in the template.

The challenge is to get this to work for multiple modal windows. I can easily assign unique IDs for each modal with k_page_name. But I'm not sure how I can create multiple buffers and then access the contents of these buffers. Any ideas? Thanks ...
Hi potato,

The if/else part of the code is fine but I am not sure why you are using the buffer.

From what I could gather from the code, whether it is a link or a button that gets displayed, depends on the data contained within the page itself (the 'modal_button' editable region).

So when the user keys in the following shortcode -
[modal name="xyz"]
- the data contained within page named 'xyz' is used to show the link/button and the window.

I don't quite see the need for a buffer here.
The windows are already uniquely named.
When the user keys in another shortcode e.g.
[modal name="abc"]
- the window created for it will have a unique id based on the pagename 'abc' which would be distinct from the first window 'xyz'.

Or is it that the user can use the same shortcode (i.e. with the same pagename) multiple times and you want to have a unique window for each instance even if the contained page is the same?

I am sorry if I could not get the requirement right. Can do with some more explanation.

Thanks.
let me give a bit more info to help explain what happens ...

Enter this in the wysiwyg editor:

One of his most famous paintings is The Scream[modal name="munch"] he's a very interesting artist.


the resulting markup is:
Code: Select all
         <a class="launch-modal" data-toggle="modal" data-target="#munch">
              find out more about Edvard Munch
            </a>   
      
      
         <div class="modal fade" id="munch" tabindex="-1" role="dialog" aria-labelledby="munchLabel" aria-hidden="true">
           <div class="modal-dialog">
             <div class="modal-content">
               <div class="modal-header">
                 <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
                 <h4 class="modal-title" id="munchLabel">Edvard Munch 1863-1944</h4>
               </div>
               <div class="modal-body">
                <p>A Norwegian painter and printmaker whose intensely evocative treatment of psychological themes built upon some of the main tenets of late 19th-century Symbolism and greatly influenced German Expressionism in the early 20th century. One of his most well-known works is The Scream of 1893.</p>
               </div>
               <div class="modal-footer">
                 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
               </div>
             </div>
           </div>
         </div> 
         
      he&#39;s a very interesting artist.</p>

which looks like this ...
modal1.gif
modal1.gif (3.99 KiB) Viewed 3810 times


i.e. all the divs for the modal window are embedded with the <p> tag. The markup for the modal window needs to be output separately from the modal trigger.

But when I can separate out the modal trigger and modal window it is possible to insert the trigger/anchor text for the modal window inline ...
modal2.gif
modal2.gif (5.32 KiB) Viewed 3810 times


And I have this working fine for just the one modal window within a wysiwyg editable region. But I'm stuck on how I could allow more than one modal window - buffer the content and then output 2 or more modal window content areas.

Hope this explains a bit more clearly - thanks!
15 posts Page 1 of 2