ryan

Hanna Code

Recommended Posts

Easily insert any complex HTML, Javascript or PHP output in your ProcessWire content by creating your own Hanna code tags.
This module is based loosely on the WordPress Hana Code Insert plugin.
 
post-2-0-11674600-1370199246_thumb.png
 
post-2-0-50116800-1370199239_thumb.png
 
A Hanna code tag looks like [[hello_world]]. A Hanna code tag with attributes looks like [[hello_world foo=bar" bar="foo]] using HTML style attributes or [[hello_world foo=bar, bar=foo]] using ProcessWire selector style attributes. After installing the module, you define your Hanna codes in Setup > Hanna Code.
 
These Hanna codes that you define can then be entered within your body copy (or other text where you allow) and they will be replaced with the values defined or generated by your Hanna code. A common use case is to embed scripts or other bits of HTML or codes that would usually be stripped out by an editor like TinyMCE. However, Hanna codes can be more than just static snippets--they can be dynamic PHP or Javascript that outputs different things according to the request. PHP-based Hanna codes have access to the entire ProcessWire API.
 
Hanna code accepts named attributes in the tag that can be passed as variables to PHP and Javascript Hanna codes. These attributes can be specified either in HTML attribute format or ProcessWire selector format. In either case, quotes should be used around the attribute value when the value contains whitespace or a comma.
 
How to install
  • Place the module files in /site/modules/TextformatterHannaCode/
  • In your admin, click Modules > Check for new modules
  • Click install for TextformatterHannaCode
  • Now to go Setup > Fields and locate the Textarea field(s) that you want to use Hanna codes with ("body" for instance). 
  • When editing the field, click the details tab, and select "Hanna Code" as the Textformatter. Save.
  • Now go to Setup > Hanna Code and start defining your Hanna Codes! You may want to use one of the examples from this document to get started.
Tag format
 
Below is a Hanna code tag named hello_world with no attributes. If you pasted this into your body copy, you would get whatever the replacement value is that you defined.
[[hello_world]]
Below is a Hanna code tag named hello_world being passed attributes of foo, bar and foobar. If this were a PHP-based Hanna code, it would receive the variables $foo, $bar and $foobar:
[[hello_world foo="bar" bar="foo" foobar="foo bar"]]
Below is the same Hanna code tag as above, but with attributes more like ProcessWire selectors. You can use whatever format you prefer. Just note that unlike regular ProcessWire selectors, quotes (single or double) are required around any value that has whitespace.
[[hello_world, foo=bar, bar=foo, foobar="foo bar"]] 
How to use
 
Please make sure that you have completed the How to install section first. Then in your admin, go to Setup > Hanna Codes.
 
Each Hanna code that you add has a type of either: Text/HTML, Javascript or PHP. The Text/HTML type is literally self explanatory in that your [[custom-tag]] is replaced with exactly the text you paste in. Anywhere that you type your [[custom-tag]] in your body copy will be replaced with exactly the static text you defined.
 
More power opens up with the Javascript and/or PHP types of codes. These codes execute at runtime and thus can contain specific logic to produce different results. In fact, PHP Hanna codes have access to the entire ProcessWire API and are executed in the same manner as template files. Your PHP-based Hanna code should simply "echo" or "print" the replacement value.
 
PHP example
  • Create a new Hanna code with the name "children".
  • Select "PHP" as the type.
  • Paste in the following for the code:
foreach($page->children as $child) {
  echo "<p><a href='$child->url'>$child->title</a>";
}
  • Now go and edit a page that has children. In the body copy, enter [[children]] in the place where you want the output to appear.
  • View the page, and you should see the rendered list of links to children.
PHP example, part 2
 
Now lets take the above example further...
  • Go back and edit your "children" Hanna code, as we are going to modify it to respond to a "parent" attribute. Change the code to this:
if(isset($parent)) {
  // If $parent is an ID or path, lets convert it to a Page
  $parent = $pages->get($parent);
} else {
  // otherwise lets assume the current page is the parent
  $parent = $page; 
}

foreach($parent->children as $child) {
  echo "<p><a href='$child->url'>$child->title</a>";
}
  • Go back and edit the page where you previously inserted the [[children]] tag, and change it to: [[children, parent=1]] (specifying the homepage) or [[children, parent=/path/to/some/parent/]] if you want to try something else.
  • View the page and you should now see it showing the children of the homepage (or of another parent you specified).
Please see the Javascript and PHP usage notes on the Hanna code entry screen.
 
Security
 
There are major security implications with a tool that will let you enter unfiltered text and code from your web browser. As a result, Hanna codes are meant for definition only by superusers and we recommend keeping it that way.
 
Download
 
Download the Hanna Code module from the ProcessWire modules page or from GitHub
  • Like 33

Share this post


Link to post
Share on other sites

Just a note to all MODx'ers - I think this is a nice way to transfer some snippets/chunks to ProcessWire as well if people want to go down that route and have some they used to use in body text. Obviously it's not quite the same thing (potentially more powerful depending on your needs), but anything that helps make a transition easier is good in my book :)

Very useful, powerful module ryan!

  • Like 3

Share this post


Link to post
Share on other sites

Wow! Great module, Ryan.

Gonna create a bunch of snippets for quick prototyping and trying things. Expect a new wave of MODx'ers on the forum. These folks love using code snippents the way Hanna Code allows it!

Edit: Pete, we had almost the same thought!

  • Like 1

Share this post


Link to post
Share on other sites

Wow! Nice one; you've been busy!

Yes, there have been two or three questions from MODx'ers in the forums about something like this - the ability to randomly, directly call snippets, chunks and TVs within the body field/area in TinyMCE. Previously they've been pointed to shortcodes and tag parser. Hanna is certainly more powerful and would solve this issue easily.

I can't wait to see the creative ways people use this :rolleyes: .

Thx for the module!

  • Like 2

Share this post


Link to post
Share on other sites

Ryan,

This is great — thank you!

Quite a lot happening around here for a Sunday.

:)

  • Like 2

Share this post


Link to post
Share on other sites

Nice addition to the growing collection of very useful modules!

  • Like 1

Share this post


Link to post
Share on other sites

Looks like a very useful module, thanks for sharing it Ryan.

I just tried to install it using ModulesManager, and I can't install it right after downloading as it requires Textformatter module to be installed.

TemplateFile: Module ProcessHannaCode requires: TextformatterHannaCode

#0 [internal function]: Modules->___install('ProcessHannaCod...')...

Couldn't it be installed when installing the process module?

Edit: When I create a new hanna code and not enter a tag name and save I get an error and can't see the form.

  • Like 1

Share this post


Link to post
Share on other sites

This looks brilliant Ryan, thank you! There are parallels with a nice aspect of Textpattern here also, TXP 'Forms' (snippets). Brill! ^_^

  • Like 1

Share this post


Link to post
Share on other sites

Looks like a very useful module, thanks for sharing it Ryan.

I just tried to install it using ModulesManager, and I can't install it right after downloading as it requires Textformatter module to be installed.

TemplateFile: Module ProcessHannaCode requires: TextformatterHannaCode

#0 [internal function]: Modules->___install('ProcessHannaCod...')...

Couldn't it be installed when installing the process module?

Edit: When I create a new hanna code and not enter a tag name and save I get an error and can't see the form.

Works fine if you use default Module manager (PW's). You can only install via TextformatterHannaCode actually. Summary says this "Also Installs - ProcessHannaCode". The install button for  ProcessHannaCode is greyed out with the message "Requires - TextformatterHannaCode". ProcessHannaCode auto installs. Maybe something with ModulesManager?

@Ryan: The TextformatterHannaCode and ProcessHannaCode have the same name - "Hanna Code" in PW module manager. This can be confusing :) (but not a big deal).

post-894-0-28482800-1370266185_thumb.png

Share this post


Link to post
Share on other sites

@kongodo, but I don't use Modules page anymore :D I didn't see that the Textformatter is the mother module. At the end it got installed but it's confusing.

Of course it has to do with ModulesManager and the nature of the modules. Modules Manager doesn't know what modules are there and how to install. It takes the module "class name" that is entered on modules.processwire.com to create folder and installs the main module. It's all information I got from modules.processwire.com and there's no manifest, dependencies or lifecycle informations to modules in PW (which IMO is requires rather sooner than later)

For some reason it seems now Ryan has changed the module class name to "TextformatterHannaCode", and this should work better now. Though changing those settings "class name" is dangerous once the module is installed via ModulesManager, because there's actually two different folders for the same module.

Share this post


Link to post
Share on other sites
I just tried to install it using ModulesManager, and I can't install it right after downloading as it requires Textformatter module to be installed.

Sorry about that, I went back and forth on the name a couple times and ultimately entered the wrong class name in the modules directory. It should have been TextformatterHannaCode, not ProcessHannaCode. I have corrected that.

Edit: When I create a new hanna code and not enter a tag name and save I get an error and can't see the form.

I was throwing an exception when I shouldn't have. I'm pushing a correction for this. 

 The TextformatterHannaCode and ProcessHannaCode have the same name - "Hanna Code" in PW module manager. This can be confusing :) (but not a big deal).

Technically they are part of the same program "Hanna Code", even if that program has two modules. But I know what you mean. I'm adding "(Process)" to the Process one, just so that the two next to each other can be differentiated. 

Also, one more section I just added to the README:

Using Hanna Code from the API
 
If you want to populate Hanna Code from the API, you can do so using the render() method, like this:
$hanna = $modules->get('TextformatterHannaCode'); 
$page->body = $hanna->render($page->body); 
  • Like 8

Share this post


Link to post
Share on other sites

Just posted version 0.0.2 that addresses the items above. It also adds an option that lets you specify whether the immediate surrounding HTML tag should be replaced with your Hanna Code output or not. For instance, if your [[hello-world]] Hanna Code output is "Hello World" and is applied to the body copy containing "<p>[[hello-world]]</p>" then you probably don't want it to replace the surrounding <p> tags. Whereas if your Hanna Code outputs block level elements like "<ul><li>Hello World</li></ul>" then you probably do want it to replace the surrounding <p> tags, otherwise you'd end up with some invalid markup like: <p><ul><li>Hello World</li></ul></p>.

  • Like 2

Share this post


Link to post
Share on other sites

I have tested Hanna with single {, ( and [, i.e. 

(hello)
[hello]
{hello} 
They all work fine in both PHP and Text mode.

Ryan, are these OK or they could throw up issues? Thx.

Share this post


Link to post
Share on other sites

I think it would be really cool if there was and easy (JSON?) way to import/export hanna codes (all or single). Maybe then we could setup a subforum to share them.

Otherwise this is amazing, thanks Ryan.

Share this post


Link to post
Share on other sites

I think it would be really cool if there was and easy (JSON?) way to import/export hanna codes (all or single). Maybe then we could setup a subforum to share them.

Otherwise this is amazing, thanks Ryan.

/site/assets/cache/HannaCode/ :). The PHP are stored as PHP files

Share this post


Link to post
Share on other sites

Is it just me or is Hanna version 1.0.4 broken? It no longer works - it outputs the raw tag :(

Don't know what's happening but even older versions do not work any more. I'm stumped. Ideas? Using it on 2.3 stable

Edited by kongondo

Share this post


Link to post
Share on other sites

/site/assets/cache/HannaCode/ :). The PHP are stored as PHP files

Haha, thats cool, I guess I could've taken a look before posting. Though a spot to officially discuss these maybe couldn't hurt. :P

Share this post


Link to post
Share on other sites

Is it just me or is Hanna version 1.0.4 broken? It no longer works - it outputs the raw tag  :(

Don't know what's happening but even older versions do not work any more. I'm stumped. Ideas? Using it on 2.3 stable

It's working for me at least, though I did change some things about the regex's in 0.0.4, so I might need to get more info about the context. What's an example of a tag that isn't working for you? What is the markup that surrounds it in your text? Also can you double check that "Hanna Code" is set as the textformatter for your body field (or whatever field you are using it in) in Setup > Fields > body > details?

Ryan, are these OK or they could throw up issues? Thx.

It should be fine to set your open/close tags to be whatever you want. Though [this] is a fairly common text construct, where as [[this]] is not. So if your [this] ever conflicts with the name of a Hanna Code, that could cause some confusion. As a result, I think it may be preferable to use open/close tags that don't duplicate things you would usually see in written copy. 

Share this post


Link to post
Share on other sites

I'm going to give this a go for inserting galleries on the fly. I think this combined with responsive CSS is pretty powerful stuff.

Share this post


Link to post
Share on other sites

It's working for me at least, though I did change some things about the regex's in 0.0.4, so I might need to get more info about the context. What's an example of a tag that isn't working for you? What is the markup that surrounds it in your text? Also can you double check that "Hanna Code" is set as the textformatter for your body field (or whatever field you are using it in) in Setup > Fields > body > details?

Sorry; I just wasted your precious time! I forgot to set it as my textformatter after I made a couple of changes!  :-[ Thanks :)

Share this post


Link to post
Share on other sites

What kills me about this module is that Ryan literally decided to make it because I had something similar in WordPress and needed a substitute solution in ProcessWire (as part of the move of CMS Critic to PW) otherwise I'd have a ton of weird shortcodes in my posts for no reason. He made this module, imported my code from the other module (called Hana Code in WP) and named his Hanna Code (after his daughter) and Poof! a module is born.

I am insanely jealous of his mad php skills (but glad they can be bought!)

  • Like 10

Share this post


Link to post
Share on other sites

And we have you to thank for hiring him in the first place which led us to Hanna Code! Thank you!

  • Like 4

Share this post


Link to post
Share on other sites

Ryan, brilliant stuff!
 

For me it won't work in my first attempt and it took me some time to figure out my failure.

You can't use the identifier "name" for a variable.

[[gallery name="Test"]]

I didn't found any comments on this issue, so perhaps it helps.

  • Like 2

Share this post


Link to post
Share on other sites

Kent, that's correct–I'll update the readme file. The term "name" is set to the name of the Hanna code. This is provided to the PHP so that it can check the name of the code, just in case it needs it. Likewise, it's preferable not to use any API variable name for your attributes either. 

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By bernhard
      @Sergio asked about the pdf creation process in the showcase thread about my 360° feedback/survey tool and so I went ahead and set my little pdf helper module to public:
       
      Download & Docs: https://gitlab.com/baumrock/RockPdf
       
      You can combine it easily with RockReplacer: 
       
    • By Soma
      LogMaintenance
      A simple ProcessWire module to give some maintenance control over log files. I found myself often having lots of log files for different things that can grow more or less quickly to a size where they can be difficult to maintain. The built in Logger of PW does a good job of giving you the possibility to delete or prune logs. But it has to be done manually and sometimes a log grows into millions of lines, which makes it often impossible to even prune it as it's too large.
      LogMaintenance uses LazyCron to run the maintenance task and there's several settings you can setup on a global or per log basis.
      Archive: will create zip files for each log file in logs/archive/ folder and add the log each time the maintenance is run to a subfolder containing the datetime.
      Lines: keeps logs to a certain number of lines 
      Days: keeps the log to a certain number of days
      Bytes: keeps the log to a certain amount of bytes
      Each setting is checked from top down, the first setting to contain something is used. So if you check the "Archive" option, all other settings are ignored and logs are archived everytime the LazyCron is executed. If you want to keep your logs to a certain amount of bytes just leave all other settings to 0 or blank.
      Per Log Settings
      There's a textarea that you can use to setup a config for a specific log file one per line. All the logs you define here ignore the global settings above. The syntax for the settings is:
      logname:[archive]:[lines]:[days]:[bytes] 
      errors:1:0:0:0 // would archive the errors log messages:0:10000:0:0 // will prune the errors log to 10000 lines  
      The module can be found on github for you to check out. It's still fresh and I'm currently testing.
      https://github.com/somatonic/LogMaintenance
    • By teppo
      Fieldtype Page IDs is a third party Fieldtype that, simply put, stores Page references as integers (Page IDs).
      This fieldtype was built as a quick and dirty workaround for Page Reference fields' inability handle self-references due to circular reference issues. A project I've been working on for a while now includes a combination of RepeaterMatrix content blocks and tagging/categorization system that would've resulted in a lot of duplicate pages (and plenty of unnecessary manual work for content editors) had I used built-in Page Reference fields, and thus a new Fieldtype felt like the most sensible approach.
      Fieldtype Page IDs was designed to be loosely compatible with Page References in order to make conversions between the two feasible, but it is quite limited feature wise:
      largely due to the fact that stored values are actually just integers with no connection to Pages whatsoever some advanced selectors and related features are not supported, and page values can't be directly accessed configuration settings are limited to the bare essentials (selector string and Inputfield class) only a handful of Inputfields (AsmSelect, Checkboxes, Text) are (currently) supported Anyway, in case you need to store Page IDs (and Page IDs only) and are happy with the limitations mentioned above, feel free to give this Fieldtype a try. It has been working fine for me in one particular project, but hasn't been tested that much, so please tread carefully – and let me know if you run into any issues.
      GitHub repository: https://github.com/teppokoivula/FieldtypePageIDs
      Modules directory: https://modules.processwire.com/modules/fieldtype-page-ids/
    • By daniels
      This is a lightweight alternative to other newsletter & newsletter-subscription modules.
      You can find the Module in the Modules directory and on Github
      It can subscribe, update, unsubscribe & delete a user in a list in Mailchimp with MailChimp API 3.0. It does not provide any forms or validation, so you can feel free to use your own. To protect your users, it does not save any user data in logs or sends them to an admin.
      This module fits your needs if you...
      ...use Mailchimp as your newsletter / email-automation tool ...want to let users subscribe to your newsletter on your website ...want to use your own form, validation and messages (with or without the wire forms) ...don't want any personal user data saved in any way in your ProcessWire environment (cf. EU data regulation terms) ...like to subscribe, update, unsubscribe or delete users to/from different lists ...like the Mailchimp UI for creating / sending / reviewing email campaigns *I have only tested it with PHP 7.x so far, so use on owners risk
      EDIT:
      I've updated the module to 0.0.4. I removed the instructions from this forum, so I don't have to maintain it on multiple places. Just checkout the readme on github 🙂
      If you have questions or like to contribute, just post a reply or create an issue or pr on github. 
    • By bernhard
      WHY?
      This module was built to fill the gap between simple $pages->find() operations and complex SQL queries.
      The problem with $pages->find() is that it loads all pages into memory and that can be a problem when querying multiple thousands of pages. Even $pages->findMany() loads all pages into memory and therefore is a lot slower than regular SQL.
      The problem with SQL on the other hand is, that the queries are quite complex to build. All fields are separate tables, some repeatable fields use multiple rows for their content that belong to only one single page, you always need to check for the page status (which is not necessary on regular find() operations and therefore nobody is used to that).
      In short: It is far too much work to efficiently and easily get an array of data based on PW pages and fields and I need that a lot for my RockGrid module to build all kinds of tabular data.

      Basic Usage

       
      Docs & Download
      https://modules.processwire.com/modules/rock-finder/
      https://gitlab.com/baumrock/RockFinder/tree/master
       
      Changelog
      180528, v1.0.4 add custom select statement option 180516 change sql query method, bump version to 1.0.0 180515 multilang bugfix 180513 beta release <180513 preview/discussion took place here: https://processwire.com/talk/topic/18983-rocksqlfinder-highly-efficient-and-flexible-sql-finder-module/