Best way to embed images in "posts"


I have a simple "post" AR class/table. Its based on the "blog tutorial" so it serves the same purpose.

I want to be able to embed images into posts. My design was as follows and I wasn’t entirely happy with it so I’ll be happy for your feedback:

Design suggestion:

[font="Courier New"]protected/data/posts_files/<post_id>[/font] would be the repository of posts "resources" (images in our case, but can be anything). Post authors need to put the used images/files in the right location (after the post is created since its id is needed). My needs are really humble so from convenience point of view this is ok. When a post that include such images is requested, CAssetManager will be called to the rescue, to "publish()" the directory of that post (e.g. [font="Courier New"]protected/data/posts_files/17[/font]).

In the posts themselves (in their "content" or "body" - which ever term you prefer), images will be embedded with <img> tags that the src attribute pointing to the published directory for this post_id in the assets folder (for example, after publishing a specific post, [font="Courier New"]WEBROOT/assets/d379e294/some_image.gif[/font]).

Problem is that when the post is created/edited, I do not know what path the CAssetManager will create and return - that seemingly random text string ([font=“Courier New”]d379e294[/font] in the example above). So, I need on runtime, when loading the ‘post’, to update its content live, after publishing has been made, with the correct url/paths in the ‘src’ attribute of the img tags (or for every other kind of resource linked in that ‘post’).

This was a little challenging: Initially I went with a PHP variable in the ‘post’ content/body and tried to somehow interpolate it when loading or just before rendering the post. I wasn’t successful. I used PHP’s [font=“Courier New”]eval()[/font], which is very bad practice and dangerous by itself, and it kept crashing complaining on the other content within the ‘post’ body.

I resorted to have some simple place holder string in the body of the posts - "ASSETS_URL" and [font="Courier New"]str_replace()[/font] it when loading the post with the published assets path that I "now" have at hand (at runtime).

I’m not sure this is the best approach. this place holder string could be needed in some post and besides, that seems like a basic templating engine start and my intuition tells me I’m missing something if I do a simple templating engine inside a framework. I just can pin point on the ‘right’ alternative, the best practice alternative, to do it in Yii.

I wanted to ask for your opinion on this - both on the design of the solution from the higher level and on the design of the implementation itself. Would you change things and make them different?

(I didn’t want to dive into KcFinder so intentionally left it out).