Forum for discussing general topics related to Couch.
4 posts Page 1 of 1
Quick question ... maybe I need coffee or it is getting late ...
I know how to display content on another page, all of it, or specific (based on name, etc).

How about specifying the other page name in the URL?

Say I have:
domain.com/blog/nice-article-1

I want to make a new file, where I only display some stuff in JSON format, like:
domain.com/API/nice-article-1

can I make the new page take the URL and output the content from blog.php "page name", based on the url that was called?

Should I be using Routes for this, or there is an easier way?

<cms:pages masterpage='blog.php' page_name='URLPARAMS'></cms:pages> or something
There are certainly some ways to do this or that, but the use-case is not clear. I suggest to do it the easiest way possible: add a querystring.
domain.com/blog/nice-article-1.html
to
domain.com/blog/nice-article-1.html?json=1

Next, cms:gpc can 'sense' the presence of 'json' in URL and you can output some code and abort the rest of the code:
Code: Select all
<cms:if "<cms:gpc 'json' />">
   <cms:capture into='json_page' >

   ... code to show your json stuff

   </cms:capture>
   <cms:abort msg=json_page />
</cms:if>


Additionally, you could tamper with .htaccess (add a rewrite rule), so whenever anyone requests 'domain.com/api/nice-article-1.html' Apache redirects user to 'domain.com/blog/nice-article-1.html?json=1' and keeps the URL in address bar as requested (with 'api').
Wouldn't it be a solution?
I don't specifically need another URL, your example with "domain.com/blog/nice-article-1.html?json=1" is much simpler for me anyway. I just couln't imagine how to do it.

I didn't know about <cms:gpc, I think this is exacly what I need. I will just output JSON instead of the normal content.

Thanks!

Edit: It works great just with your example. This community is awesome :)
I am just glad it helped :)


For anyone stumbling across this thread and wondering about .htaccess rule, here is a sample:

Code: Select all
<IfModule mod_rewrite.c>
RewriteEngine On

#If your website is installed in a subfolder, change the line below to reflect the path to the subfolder.
#e.g. for http://www.example.com/subdomain1/subdomain2/ make it RewriteBase /subdomain1/subdomain2
RewriteBase /


#CUSTOM REQUEST: projects/16948 OR projects/16948/ DESTINATION: projects-view.php?rid=16948
RewriteRule ^projects\/(\d+?)\/??$ projects-view.php?rid=$1 [L,QSA]

</IfModule>


I had to use such technique to maintain backward compatibility with some older software that needed to access couch-powered website by urls in a special format. While routes would do it as well, rewrite rule is sooo much faster and easier to do.

In the above sample, user types in browser or maybe some software requests following link: http://site/projects/16948
In reality, request is being redirected invisibly for user to link http://site/projects-view.php?rid=16948.

Hope it helps further on. ;)
4 posts Page 1 of 1