Jump to content

Hanna Code


ryan

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
Link to comment
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 4
Link to comment
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
Link to comment
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
Link to comment
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
Link to comment
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

Link to comment
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.

Link to comment
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
Link to comment
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
Link to comment
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

Link to comment
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
Link to comment
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. 

Link to comment
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 :)

Link to comment
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
Link to comment
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
Link to comment
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. 

Link to comment
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
×
×
  • Create New...