Part 4 of the tutorial and PHPTemplate plugins

3 views
Skip to first unread message

Nick Lo

unread,
Sep 15, 2010, 6:17:49 AM9/15/10
to cobweb-dev
After reading...

"Our very simplistic wiki is almost done, but we have yet to create
the interface and code necessary to create and edit the wiki pages. We
will do just that i part 4 of this tutorial!"

...I wondered if there is any chance of you doing that as I'm very
keen to see how your form code should work.

Also, could you give some indication of how the plugins work for
PHPTemplate. I notice that you have an URL plugin which I presume
works like the django URL template tag, but even after looking through
the source code I couldn't get an idea how to use it.

Great job on the framework, by the way, I'm using it on a PHP site
that ideally I'd like to convert to Django but Cobweb is tidying it up
nicely.

Øystein Riiser Gundersen

unread,
Sep 15, 2010, 2:53:35 PM9/15/10
to cobwe...@googlegroups.com

Den 15. sep. 2010 kl. 12.17 skrev Nick Lo:

> After reading...
>
> "Our very simplistic wiki is almost done, but we have yet to create
> the interface and code necessary to create and edit the wiki pages. We
> will do just that i part 4 of this tutorial!"
>
> ...I wondered if there is any chance of you doing that as I'm very
> keen to see how your form code should work.

I am quite busy at the moment, but I have plans to improve the Cobweb documentation (including the tutorial) in the coming months. In the meantime, here is an more-or-less complete example of a create/edit form using Cobweb's forms framework (untested, also, make sure to use trunk): <http://gist.github.com/581199>

Cobweb's forms framework is similar to Django's newforms with concepts such as fields, widgets etc. Have a look in <http://code.google.com/p/cobweb/source/browse/#svn/trunk/forms> directory for the code. I have yet to implement some of the more complex stuff, such as fieldsets, `ComboField` and -- importantly -- `ModelForm`, but plan to add them in the future.


> Also, could you give some indication of how the plugins work for
> PHPTemplate. I notice that you have an URL plugin which I presume
> works like the django URL template tag, but even after looking through
> the source code I couldn't get an idea how to use it.

The PHPTemplate templates are really just run as PHP code, so any callable can work as a template plugin. The plugin layer is there mostly to provide a mechanism for easily loading additional template functionality (like Django's {% load foo %}) without polluting the function namespace with lots of generic names such as `url()`, `escape()` etc.

To use the URL plugin, put `$_->load('url');` at the top of your template and use it like this:

<a href="<?php echo $_->url('wiki.wiki.page', array('wiki_word' => 'Word')) ?>">Word</a>; <!-- works like Django's `url` template tag -->

---

Here is a brief explanation: (...looking back at it, the plugin system is probably a bit over-engineered, but your mileage may vary :)

A template plugin is a class which implements a `__plugins()` method, which takes the template adapter as its argument and returns an array of mapping between method name to call and a corresponding template "tag" name:

<?php // escape.template_plugin.php <- note the naming convention

class EscapeTemplatePlugin implements PHPTemplatePlugin {

public function escapeHTML($string) { // return escaped $string }
public function escapeURL($string) { // return escaped $string }
// ...and so on

public function __plugins(TemplateAdapter $adapter) {
return array(
'escapeHTML' => 'esc_html',
'escapeURL' => 'esc_url',
// ...and so on
);
}
}

After calling `$_->load('escape')` in the template, `esc_html` and `esc_url` are available as methods on the `$_` object.

> Great job on the framework, by the way, I'm using it on a PHP site
> that ideally I'd like to convert to Django but Cobweb is tidying it up
> nicely.

Glad to hear it! :)

--
Øystein Riiser Gundersen
oyste...@gmail.com

Nick Lo

unread,
Sep 17, 2010, 7:47:55 AM9/17/10
to cobweb-dev
> In the meantime, here is an more-or-less complete example of a create/edit form using Cobweb's forms framework (untested, also, make sure to use trunk): <http://gist.github.com/581199>

Thanks, this was really useful and I spent yesterday reworking an old
clunky form using Cobweb. I didn't need much more than the example you
gave as you've clearly followed Django quite closely.

> <a href="<?php echo $_->url('wiki.wiki.page', array('wiki_word' => 'Word')) ?>">Word</a>; <!-- works like Django's `url` template tag -->

This works but I ran into an issue where I had two different urls
pointing to the same controller action. In Django, named urls would've
taken care of that but on a relatively brief look through the code it
doesn't appear that Cobweb has them?

Øystein Riiser Gundersen

unread,
Sep 17, 2010, 8:32:22 AM9/17/10
to cobwe...@googlegroups.com

Den 17. sep. 2010 kl. 13.47 skrev Nick Lo:

>> In the meantime, here is an more-or-less complete example of a create/edit form using Cobweb's forms framework (untested, also, make sure to use trunk): <http://gist.github.com/581199>
>
> Thanks, this was really useful and I spent yesterday reworking an old
> clunky form using Cobweb. I didn't need much more than the example you
> gave as you've clearly followed Django quite closely.

Excellent :) Yes, the forms code is more or less an attempt to port `django.forms` to PHP.

>> <a href="<?php echo $_->url('wiki.wiki.page', array('wiki_word' => 'Word')) ?>">Word</a>; <!-- works like Django's `url` template tag -->
>
> This works but I ran into an issue where I had two different urls
> pointing to the same controller action. In Django, named urls would've
> taken care of that but on a relatively brief look through the code it
> doesn't appear that Cobweb has them?

Cobweb supports named URL patterns, it just isn't documented yet -- I will remember to include that in the part 4 of the tutorial.

Using the wiki example, consider a controller action that acts as an endpoint for both creating new pages and updating existing pages:

<?php
public function edit($wiki_word = NULL) { /* ... */ }

Then, in the URL configuration:

<?php
return array(
'^page/(?P<wiki_word>\w+)$' => 'wiki.wiki.page',

'^page/(?P<wiki_word>\w+)/edit$' => 'wiki.wiki.edit',
'^page/create$' => array('wiki.wiki.edit' /* controller action */, array('name' => 'wiki.wiki.create') /* options */)
);

Now, both `wiki.wiki.edit` and `wiki.wiki.create` URLs will resolve to the same action, but result in different URLs when using url(). Also, `wiki.wiki.edit` will require the $wiki_word argument, where as `wiki.wiki.create` will not and its value will be set to NULL.

See also `Router::reverse($pattern_name, array $arguments)` which is the what url() uses internally, only url() swallows exceptions and returns '#' for unresolvable patterns.

---

By wrapping the action rule in an array, you can also "fill in" default arguments for the action:

<?php
return array(
'^me$' => array('wiki.wiki.page', 'wiki_word' => 'Øystein Riiser Gundersen', array('name' => 'mypage'))
)

Calling url('mypage') will return the URL "/me" and resolve to the page() method called with "Øystein Riiser Gundersen" as the value of the $wiki_word argument. This feature is described in further detail in this article: <http://code.google.com/p/cobweb/wiki/URLDispatch>

joylin

unread,
Sep 23, 2010, 8:17:32 AM9/23/10
to cobweb-dev
Hi

I am trying to use form framework. The example uses php tags. Can you
please tell me how to do it using h2o template tags.

Thanks

On Sep 15, 11:53 pm, Øystein Riiser Gundersen <oystein...@gmail.com>
wrote:
> oystein...@gmail.com

Øystein Riiser Gundersen

unread,
Sep 25, 2010, 3:07:50 PM9/25/10
to cobwe...@googlegroups.com

Den 23. sep. 2010 kl. 14.17 skrev joylin:

> Hi
>
> I am trying to use form framework. The example uses php tags. Can you
> please tell me how to do it using h2o template tags.

(Note: I have never really used H20 myself, so take this advice with a grain of salt :)

From reading the documentation <http://wiki.github.com/speedmax/h2o-php/quick-start> you can just use H20's dot notation to access forms and form fields:

An untested translation of the template example in <http://gist.github.com/581199> follows:

<form action="edit<?php if ($word->exists()): echo '/' . $word->id; endif ?>">
<?php if ($request->isPOST() && !$form->isValid()): ?>
<div class="validation-error">The form is invalid, bla, bla.</div>
<?php endif ?>
<?php foreach ($form as $field): ?>
<div>
<?php
echo $field->renderLabel();
echo $field->render();
?>
<?php if (!$field->isValid()): ?>
<div class="validation-error">
<?php echo implode($field->errors(), '<br />' ?><!-- "Enter a valid slug.", "This field is required", etc -->
</div>
<?php endif ?>
</div>
<?php endforeach ?>
<input type="submit" />
</form>

<form action="edit{% if word.exists %}/{{ word.id }}{% endif %}">
{% if request.isPOST and form.isValid %}
<div class="validation-error">The form is invalid, bla, bla.</div>
{% endif %}
{% for field in form %}
<div>
{{ field.renderLabel }}
{{ field.render }}
{% field.isValid }
<div class="validation-error">
{{ field.errors | join <br /> }}
</div>
{% endif %}
</div>
{% endfor %}
<input type="submit" />
</form>

This code assumes that the $form variable is bound to the template. Also, you can access individual fields using dot/-> notation:

{{ form.title.render }} // should output <input type="text" name="title" id="id_title" value="" />

Hope this helps,

> --
> You received this message because you are subscribed to the Google Groups "cobweb-dev" group.
> To post to this group, send email to cobwe...@googlegroups.com.
> To unsubscribe from this group, send email to cobweb-dev+...@googlegroups.com.
> For more options, visit this group at http://groups.google.com/group/cobweb-dev?hl=en.
>

Reply all
Reply to author
Forward
0 new messages