Feature "read more" in a default blog index page

5 views
Skip to first unread message

Leandro - ProfessionalIT

unread,
Jan 6, 2010, 6:40:29 AM1/6/10
to web2py-users
Hi,
Well, I am developing my blog and in my index page I have the latest
05 posts. This posts can be big and I think that a good idea is
"break" the content and put a link "read more" to read the full
content of the post.

To solve this problem I thought of two solutions:

1) Break the post in determined number of the words, but this has a
problem:
- Break the formatation of my index page.

I implemented this to test but as the content field is a field
that accepts html tags if I put a "read more" in 50 words and this
occurs before closing a tag </ pre> eg just breaking the layout of the
page.

2) Implement/put a "special tag" [more] for example in the field
content of the post, and in the page filter to stop the post in this
tag. (Think that this is the "best idea", but I don't know how to do
this)

Then, what is the best way to do this? any suggestion or idea?

-- Leandro.

Leandro - ProfessionalIT

unread,
Jan 6, 2010, 6:47:31 AM1/6/10
to web2py-users
>   To solve this problem I thought of two solutions:
>
>   1) Break the post in determined number of the words, but this has a
> problem:
>      - Break the formatation of my index page.
>
>      I implemented this to test but as the content field is a field
> that accepts html tags if I put a "read more" in 50 words and this
> occurs before closing a tag </ pre> eg just breaking the layout of the
> page.

My implementation in the "for" that iterate in posts list:

{{if len(post.content) > 500:}}
{{=XML(post.content[0:500])}} [ read more ]
{{else:}}
{{=XML(post.content)}}
{{pass}}

Thadeus Burgess

unread,
Jan 6, 2010, 7:53:47 AM1/6/10
to web...@googlegroups.com
Since you are including html in your posts, it is a bit difficult...

I am using markdown for blogitizor, so the html always comes out clean...

snip = 2000 #num characters by default

{{=WIKI(page.content[:snip], extras={
'code-color': {'noclasses': True},
'footnotes': None,
'code-friendly': None,
})
{{if snip != -1:}}
{{=A('{...(more)...}', _href=url)}}
{{pass}}


In your case however, we need to find the index of [more] and slice
everything past it out.

i = post.content.find('[more]')
content = post.content[:i] + A("more", href=URL(...))

-Thadeus

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

Leandro - ProfessionalIT

unread,
Jan 6, 2010, 9:21:06 AM1/6/10
to web2py-users
Great Master Thadeus !, perfect ! solved my problem !.

Thank you very much !

-- Leandro.

mdipierro

unread,
Jan 6, 2010, 10:41:02 AM1/6/10
to web2py-users
Two problems with this:

{{if len(post.content) > 500:}}
{{=XML(post.content[0:500])}} [ read more ]
{{else:}}
{{=XML(post.content)}}
{{pass}}

1) XML(post.content) is vulenrable to XSS injections. Do XML
(post.content,sanitize=True) instead.

2) XML(post.content[0:500]) may (and will) truncate some tags. What if
for example post content[0:500]='bla bla ... bla <a href="http' it
will mess up your page very badly. sanitize=True will fix the problem
in this case but the output will not look nice as you's expect.


On Jan 6, 5:47 am, Leandro - ProfessionalIT <lsever...@gmail.com>
wrote:

Leandro - ProfessionalIT

unread,
Jan 6, 2010, 3:45:37 PM1/6/10
to web2py-users
> 1) XML(post.content) is vulenrable to XSS injections. Do XML
> (post.content,sanitize=True) instead.

I'll do it

> 2) XML(post.content[0:500]) may (and will) truncate some tags. What if
> for example post content[0:500]='bla bla ... bla <a href="http' it
> will mess up your page very badly. sanitize=True will fix the problem
> in this case but the output will not look nice as you's expect.

Now, I define a comment <!--more--> where I want generate the break of
the post in my RichText Editor. This solved my problem.

thanks for any help

-- Leandro.

Reply all
Reply to author
Forward
0 new messages