Flapping Head » Web Development http://scottbarnham.com/blog Code and comments on web development, Django, Python and things (un)related. Wed, 22 Jun 2016 04:22:53 +0000 en-US hourly 1 http://wordpress.org/?v=4.3 Perch blog post how to get the excerpt http://scottbarnham.com/blog/2013/09/02/perch-blog-post-how-to-get-the-excerpt/ http://scottbarnham.com/blog/2013/09/02/perch-blog-post-how-to-get-the-excerpt/#comments Mon, 02 Sep 2013 11:57:32 +0000 http://scottbarnham.com/blog/?p=114 find_by_slug($blog_slug); $excerpt = $post_object->excerpt(); // $excerpt is an array with 'raw' and […]]]> I’m not a huge fan of Perch, but I’ve used it for one client. Getting the excerpt for a blog post isn’t straight-forward. Here’s a way to do it using the API.

$api = new PerchAPI(1.0, 'perch_blog');
$posts = new PerchBlog_Posts($api);
$post_object = $posts->find_by_slug($blog_slug);
$excerpt = $post_object->excerpt();
// $excerpt is an array with 'raw' and formatted versions.
// e.g. do something with $excerpt['raw']

Hope it saves you some digging.

]]>
http://scottbarnham.com/blog/2013/09/02/perch-blog-post-how-to-get-the-excerpt/feed/ 0
Emulating Django blocks with Smarty capture http://scottbarnham.com/blog/2010/01/02/emulating-django-blocks-with-smarty-capture/ http://scottbarnham.com/blog/2010/01/02/emulating-django-blocks-with-smarty-capture/#comments Fri, 01 Jan 2010 23:34:15 +0000 http://scottbarnham.com/blog/?p=41 Django blocks considered addictive

I do Django development for Red Robot Studios and one of the many great things about the Django web framework is the template system.

Using blocks and inheritance, repetitive html is kept to a minimum. For example you can do:

base.html

...
<title>{% block title %}Default Title{% endblock %}</title>
...
<div id="content">
{% block content %}{% endblock %}
</div>
...

home.html

{% extends 'base.html' %}{% block title %}Home Page Title{% endblock %}
{% block content %}
Home page content here
{% endblock %}

The content of the blocks in home.html are plugged in to the block placeholders in base.html.

Smarty doesn’t have blocks like Django

Update: Wait a minute. I’m wrong. Smarty does have blocks, as Richard pointed out in the comments. Uh, how did I miss that?

Smarty is a template system for php. I was using it recently for a client and wished I could use Django-style blocks. Smarty doesn’t have blocks and inheritance, but does have capture and include. Here’s how I was able to achieve a similar result.

Using Smarty capture to emulate blocks

Using Smarty’s capture and include functions, here’s how the templates look:

header.tpl

...
<title>{if $smarty.capture.title}{$smarty.capture.title}{else}Default Title{/if}</title>
...
<div id="content">
{$smarty.capture.content}
</div>
...

home.tpl

{capture name='title'}Home Page Title{/capture}
{capture name='content'}
Home page content here
{/capture}

{include file='header.tpl'}

Simple, no? Remember to do these captures before including the file.

It’s not as powerful as Django template inheritance, but it’s a reasonable attempt to use Django-style blocks in Smarty templates.

]]>
http://scottbarnham.com/blog/2010/01/02/emulating-django-blocks-with-smarty-capture/feed/ 1