We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

Adding links on display to text from model

Hi,

I am making a twitter clone. What I need to do is, when displaying the contents of a post/'tweet' I need to insert links to a users page wherever he is mentioned (@<username>) and link to a hashtag wherever it is mentioned (#<hashtag>).

It occured to me I could parse the contents in the Post models beforeSave() function and add the html tags to be stored in the database, but the problem with that is that is if I change the routing I'll have a database full of incorrect links, and I need the database to be without them for the API.

I could also hardcode the path up to the <username> or <hashtag> part, but that just seems like a bad idea. So how should I do it?



93.7k
Accepted
answer
edited Aug '16

I would test multiple options, but most likely I would make a Custom Volt Filter or Function for printing the post. In that filter I would parse and replace with url, this way you will always have relevant routes and usernames if user changed something.

Something like Smileys if you used them before.. you store :) in DB but you output an image to users.