Generico Filter

Filters ::: filter_generico
Maintained by Justin Hunt
Generico is a simple filter for creating templates of code snippets and text that can be inserted into Moodle text areas.
Latest release:
2141 sites
500 downloads
77 fans
Current versions available: 2

Generico is a simple templating filter, that allows you to set up text and code snippet templates for use in Moodle text areas. It allows an unlimited number of templates to be registered. When Moodle encounters a Generico filter string it will use the data in the filter string to fill out the template, and insert it into the page. 

It is useful for ad-hoc template purposes where writing a new plugin would be overkill, or just for re-using common pieces of text. It can be used to centralise and manage the embedding of iframe content from video hosting services, and educational app providers.

In the latest version there are approx. 20 presets from which you can make templates. They include templates for an info box, tabset, accordian, lightbox and image gallery. And there is a companion Atto editor plugin for Generico. The combination of these two features make it possible to use Generico like the Wordpress shortcode plugins.

This 8 minute video walks through the basic Generico Filter and should give you a good idea of what it is, and how it works. 



How it works

Templates for use with the filter are available at:

Site Administration -> plugins -> filters -> Generico -> templates

A template primarily consists of a "key", a "template," and some "defaults." (It can also load CSS and Javascript. But for now lets keep things simple.)

The key is just a one word name, that tells Generico which template to use. 

The template is just passage of text that you want to use, and the parts of the template that you want to mark as variables you surround in @@ marks.

The defaults are a comma delimited list of variablename=value pairs. It is possible to specify multiple values for a single variable in the defaults. The Generico Atto editor plugin will display these values in a drop down list. eg size="100|120|140".  The first of the values will be the true "default."

Here is an example template.

templatekey: wildthings

template: Inside this box are @@thing1@@ and @@thing2@@

template defaults: thing2=Silly


A possible filter string for this "wildthings" template would look like this:

{GENERICO:type=wildthings,thing1=Sally}


Generico would replace the above filter string with:

"Inside this box are Sally and Silly"


The filter string must follow this format,

{GENERICO:type=templatekey,variable1=data1,variable2=data2}


The wildthings example above is trivial of course. Instead imagine using it to embed YouTube videos by registering the standard iframe code YouTube gives you, as a template. Then it would only be necessary to insert the id of the video in a generico filter string and the template would do the rest. e.g

{GENERICO:type=youtube,id=ABC12345678}


USING PRESETS

Generico comes with about 20 template presets. By default they are not set up as templates yet. To create the actual template:

  1. Open an empty template settings page
  2. Select a preset from the dropdown list at the top of the page
    This will fill in the fields on the template settings page.
  3. Press the "Save Changes" button at the bottom of the page.


USING BUNDLES

So that people can share the templates they have made, there is a system to share them called "bundles." On each template settings page there is a green bundle box.


If you click on the box, it will automatically download the "bundle" as a text file. If you then drag that bundle (or somebody else's bundle) onto the green bundle box of another template, it will fill the fields on that template settings page. Be sure to save the template after you have done that.

There are a number of bundles that people have shared over in the this forum thread. 
https://moodle.org/mod/forum/discuss.php?d=324771


And there is a special category here on the Moodle plugins directory for sharing templates: 
https://moodle.org/plugins/browse.php?list=category&id=69



USER PROFILE VARIABLES

It is also possible to add user profile variables to your templates. Just make the first part of the variable name USER: and the next part the name of the user profile field. It also works with custom profile fields. There are two "special" user profile fields, picurl and pic that respectively output the url of the user's profile pic, and their picture itself.

e.g

User's first name: @@USER:firstname@@

User's ice cream preference (custom profile field): @@USER:icecreampref@@
Users profile pic url: @@USER:picurl@@

User's profile pic:  @@USER:pic@@


INSTALLATION

Generico installs like any Moodle plugin. Please see here for more information on that process:
https://docs.moodle.org/en/Installing_plugins#Installing_directly_from_the_Moodle_plugins_directory

On the final page of the installation you will be able to register templates. its best to skip that step, and just press "save changes." Later you can do this from the Generico template settings pages.


After installing you will need to enable the Generico filter. You can enable the Generico filter at:

Site Administration -> plugins -> filters -> manage filters

More Documentation

https://docs.moodle.org/31/en/filter_generico

https://poodll.freshdesk.com/solution/articles/19000083258-how-do-widgets-work-for-developers-


Screenshots

Screenshot #0
Screenshot #1
Screenshot #2

Contributors

Justin Hunt (Lead maintainer)
Please login to view contributors details and/or to contact them

Comments RSS

Comments

  • Sat, May 22, 2021, 1:55 PM
    Hi Dominique. The reason that will not work is that the filters are processed by PHP when the page is prepared and before it is sent to the browser. At that time any CSS or JS that the template deems necessary is also added to the page. Your script is running in the browser after all the processing is over and there is no way to send the template back up to PHP for another round (that I know of).

    The way you really want to do this, is to add that JS to set content to the div, inside the template. Or even just add the content to the template body. When the filter string is put on the page, and the page displayed your template will be run and any content/js or CSS will be sent to the browser. Encapsualting all your login in the template is how it is supposed to work. You can do quite a lot within Generico, but just get the basics working first.
  • Picture of Dominique Bauer
    Tue, May 25, 2021, 1:13 PM
    Hello Justin,

    Thank you very much for taking the time to answer me. My question sounded a bit silly, but I wasn't sure. Now I am in the right direction.

    Your plugin is awesome.
  • Wed, Jun 16, 2021, 10:37 PM
    Hi there,

    Do you know if there's an ETA on a fix for the bug that only passes the first set of variables specified in the Dataset variables to the plugin.

    Love the plugin so far, it's really helpful!

    Richard


  • Thu, Jun 17, 2021, 2:24 PM
    Hi Richard, Its not really as you describe. The full set of values returned from a DB query are returned in the dataset. Only the first set of these is available in the template body, because we would need to figure out a way to reference the array of data from there, since its just html not js. And there is no way to loop from html either.

    So if your query returns an array of data you need to access it from the JS field. And set it to HTML from there. The results will be in the @@DATASET@@ variable.

    Perhaps you are referring to the situation where your query does not return a unique ID as the first field in each row in the query result set. In that case when it put into json and back out again everything gets written to row 0 in the array (ie you just get one row). In that case you should make sure your result set contains a unique id as the first field in the result set.
  • Thu, Jun 17, 2021, 8:51 PM
    Hi Justin. Apologies, I think I misread from the Moodle Docs. I've been having a bit more of a play with it and it's been working perfectly. This plugin's been an absolute life saver!
  • Sat, Jul 24, 2021, 3:39 AM
    Hi Justin. Loving the Plugin, it's really powerful. Just a quick (hopefully) question. I know a user can set the variables when they select the template in the Genero Atto plugin (or just type it), and I know you can also set a default for variables, but is there any other way to change the variable?

    Example: I have some text in a p tag, and I basically want THAT text to be the content for one of the variables. I've got JQuery to isolate and copy the text, but I can't seem to figure out a way to get this text to become the variable (so a subsequent SQL query uses this new variable in it's select statement).

    Hope that all makes sense.

    Cheers.

    Richard
  • Thu, Aug 5, 2021, 5:29 PM
    Hi Justin,

    Been doing some experimentation and it looks like it's removing everything after (and including) the decimal place. I just did a simpler "SELECT ? as result" and when I input 1.20 I got a result of 1. Doesn't seem to be doing it to any other decimal places that have other characters (eg with a letter first 'D1.5'). I thought maybe it was a data type in the table of the database but it's not that.

    Anyway, just letting you know that's an issue I have my end at the moment.

    Cheers,

    Richard
  • Thu, Aug 5, 2021, 5:41 PM
    Hi @richard, yeah it looks like some sort of bug. I will have a look when I can (today tomorrow),and post back here. Thanks for sticking with it
  • Thu, Aug 5, 2021, 6:48 PM
    Ah that's great thanks. Apologies for sending so many comments! I deleted them once I figured out what was going on, so apologies if your inbox went crazy yesterday!
  • Fri, Aug 6, 2021, 3:33 PM
    @richard I just released a new version which ought to fix this
  • Fri, Aug 6, 2021, 4:56 PM
    Hi again Justin,

    That's great news!

    Annoyingly the Moodle site I manage is provided to us by a third party and they handle plugin updates and installs (ugh). I've raised a ticket with them to get it updated and I'll keep you in the loop.

    Thanks for the swift response and fix. We're doing some really powerful stuff with this (fantastic) plugin.

    Cheers,

    Richard
  • Fri, Aug 6, 2021, 6:09 PM
    Hi again (again),

    All working great this end! Thanks for the super speedy fix!!

    Richard
  • Wed, Aug 18, 2021, 5:03 PM
    Hi Justin,

    Everything's been going well! Think I may have spotted another bug?

    When I have decimal number values that end in a 0, the 0 isn't being passed. For example, 21.10 becomes 21.1. A couple of my templates need to return a single row, so if I have a value of 21.1 and 21.10 somewhere in the same column, I'll get an error.

    I'm getting round it by adding a space next to the value in the database, and then adding that space when I use it in the filter (I guess this then turns it into a string as it's being passed?).

    Eitherway, just thought I'd let you know.

    Cheers again,

    Richard
  • Fri, Sep 17, 2021, 7:27 PM
    Hi there! First of all, thank you very much for this awesome plugin! I'm building my complete moodle structure using it.

    But, I came across one issue: Is there any possibility to use (Stash) shortcodes in within Generico Templates?
    [stashdrop secret="yECVEk" text="Pick up!" image]
    I've tried -almost- everything, but didn't work, I guess Atto has to render that shortcode, but it's not working inside Generico! Maybe including some js/library inside the template??

    I know I can just put them between tag and _endtag but I will have so many courses that that's really life changing... more if at any time on the future I can change it only with changing the Template! Thank you very much! I really will apreciate it!
  • Sat, Sep 18, 2021, 7:19 AM
    Hi Repasemos, Though I never did it, I think you can just put your stash tags in the body of the Generico template. And move the Generico filter higher (ie earlier) than Stash in the list of filters under site admin -> plugins -> filters-> manage filters. Then Generico filter will put the stash tags on the page, and Stash will process the stash tags shortly after.
Please login to post comments