0

I have a template containing sections and I'd like to process just the tagdata that is contained between the tags related to the button, and only when the button is selected (not when the page renders):

{exp:plugin:foo}
 <button>process</button>
    some content….
{/exp:plugin:foo}

some other content
{exp:plugin:foo}
 <button>process</button>
    some more content….
{/exp:plugin:foo}

I need to pass the relevant content (the tagdata from between the same tags as the button that has been pressed) to a custom function where I process it (to create a file), but I need to be able to pick out the data from within the selected tags, not the whole page, and I need to trigger this event after the page has fully loaded.

How do I deal with the button action, calling 'ACT=' doesn't let me use '$this->EE->TMPL->tagdata'?

Is there an easy way to parse and pick out the tagdata I need once the template data is available?

2
  • Could you be more specific about what you're trying to do? Example: I'm not sure if you need to dump tag data in JS and then pass it back through a module then render on AJAX response etc... What you're trying to accomplish isn't very clear, and why you are trying to accomplish it would help a lot. – jrothafer Jan 7 '16 at 14:38
  • I've added some more detail, does that help? – wot Jan 7 '16 at 15:42
2

Thanks for adding some details, but I'm still slightly confused. I'll lay out an attempt (too long for a comment anyways).

You have some content that you want processed (into a file format?), but not until a browser event is triggered. Some clarification about ways to write modules for ExpressionEngine may help.

A module can return data in multiple ways. One way is to return tag data when the template engine calls your module. Another way is to use ACTion id's to return data via AJAX and/or GET/POST requests. A third way (not pertinent) is to import your module into other modules, or extract your logic into a library that can be included in different places in an addon.

You want to process some data presented in a template, but also want to use the same logic to be able to render some data on the fly on an already rendered template. Your module can do this with basically little modification.

So one way to get params to be processed is to get it selectively from the request context:

if (AJAX_REQUEST) {

    // get from AJAX param
    $my_param = ee()->input->get_post('my_param');
} else {

    // get from template param    
    $my_param = ee()->TMPL->fetch_param('my_param');
}

From there, you simply proceed as you normally would with your module method's logic. When it comes time to return that information, you can do so, again, with the appropriate method based on request context:

if (AJAX_REQUEST) {

    // return (make sure this is JSONified for ease of use)  
    ee()->output->send_ajax_response($this->return_data);
} else {

    // return template tag data
    return ee()->TMPL->parse_variables(ee()->TMPL->tagdata, array($this->return_data));
}

So with this methodology, you can use the same method to process and present information when the template is rendered but you can also use a jQuery AJAX request to that ACTion id (with the parameters as part of the request instead of as template params) to do the same thing.

If you don't need to reuse your logic, then this answer won't help you. If you simply need to asynchronously process data, just use jQuery and AJAX to hit a module method.

<script>
$(document).ready(function() {
    $("#my_button").click(function() {

        // get info from already rendered stuff
        var my_param = $("#my_already_rendered_element").val();

        $.get( '/?ACT=12345', {my_param:my_param}, function(data){

            // dump returned dynamic info into an element
            $('#my_dynamic_element').val(data.dynamic_return_var);
        });

    });
});
</script>

If this isn't helping, add another edit to your question that clearly maps out the data path you're seeking, as I may not be understanding you still.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.