Rendering a response from a URL to RSS format - url

I am creating a controller that receives certain parameters from an application, then accesses a hard coded URL. Upon receiving a response from the URL my controller should render this response to RSS format.
In doing this I decided to use XPath to sort of create the xml tags, I then used StringBuilder to append these tags and then rendered the result as text. This is able to show on the browser just how I want it.
However when I try to view the page source it does not show any tags or headers, it just shows it as normal text on a page. I need help with what to do so that the headers and tags can appear in the page source.

I would suggest having your controller send the data as JSON, and then creating a template that renders the JSON as rss2/xml. For best results, make your JSON structure easy to loop over to create the RSS feed by looking at how feeds are organized.
Here's the rss2 spec
make sure that this line is at the top of your file with NO leading spaces
<?xml version="1.0"?>
Also make sure your content is served with "text/xml" as its content-type. In php, one would set this as such:
header('Content-Type: text/xml');
See http://www.electrictoolbox.com/rss-php-content-type/

Related

Kirby CMS: how to return Blocks as HTML?

I'm playing with Kirby CMS and using it as a headless CMS. I'm using Blocks field to handle rich content.
In the API, this field is returned as an array of objects:
[
{
"content": {
"level": "h2",
"text": "test"
},
"id": "02d4a71b-cd02-4382-a78c-84ff1e68324b",
"isHidden": false,
"type": "heading"
}
]
Which is great, I think. But in my case, it's annoying, as I should do all the rendering by myself.
Is there a way, or an option, to tell Kirby to render this field as HTML content?
I know there is a $blocks->toHtml() method, but I'm using a standalone React frontend, so I can't use this in my JSX.
You don't have to use the Kirby API directly in order to get content from your Kirby site. Kirby has a few different ways to respond to an ajax request - and that response can be in any format, such as JSON, an HTML snippet, or anything else. Here are a couple of the most often-used ways to achieve what you want.
CUSTOM ROUTE
You could define a custom route, which is like a 'custom api point', that your React interface could send requests to, and which would respond with the block list as an HTML snippet, as required. You could set your route at a URL like:
https://example.com/block-api
In your route, you could then use the toHTML() method, or a snippet(), or any other method, to convert the blocks into HTML that you can then send back as a string in your response.
In your route definition, you could also use a pattern that would allow you to (optionally) request a single block by ID. Your custom route URL for that would look something like this - where the last segment is the block ID:
https://example.com/blocks-api/02d4a71b-cd02-4382-a78c-84ff1e68324b
CUSTOM CONTENT REPRESENTATION
When you try to reach a page in your Kirby site via its normal URL, Kirby will put the page's content into your page's template, and return it as an HTML document. That is the 'default' behaviour. You can, however, override that.
You can define a custom content representation for your page, which tells Kirby that you want to receive the content of your page in a 'different format' - such as JSON, XML, or anything you wish. That 'different format' can even be just an HTML snippet. All you need to do is define a new template for it.
In your case, you could create a content representation that tells Kirby to return a fully-formatted HTML snippet of your blocks only - by creating a template called something like "mypage.blocks.php", with just the HTML for the blocks in it. So, if to access the page normally we'd go to URL...:
https://example.com/mypage
...to get your 'blocks' custom content representation for that page, you'd now go to:
https://example.com/mypage.blocks
Your custom content representation can have its own controller, too. This is useful if you want to get Kirby to send you the HTML code of just one single block, instead of all blocks on the page. You could use a query parameter in your URL to tell the controller which block you want the HTML for. The controller would then check the URL for the query parameter, and if the parameter is there, it returns only the required block. This would enable your React app to get the HTML code for just a single block on the page, by sending a request to an URL like:
https://example.com/mypage.blocks?id=02d4a71b-cd02-4382-a78c-84ff1e68324b

Posting to a web form and catching results from JavaScript code

How would I go about achieving the following
I have some HTML data triggered from an Evernote new note action
I need to pass this HTML string to a website via an http post with form variables
I then need to catch the resulting web page text in a variable to use in my next action.
For clarity the website simply takes some HTML and converts it to markdown passing it back out as the body of the resulting page
Regards in advance
Dan
Sweet! So I've got this working. In my example, text is some html I pulled out of a dummy previous step:
The output is:
Which has the markdown as a key. If you want to tweak other data based on the api params, you can do that as GET params below html
​Let me know if you've got any other questions!

Json vs html.erb in ruby

My project is on ruby on rails.
I have one of ruby api returns json.jbuilder.
But I want that my api should not return json , it will display html.erb file.
Is anybody can help me???
First of all, it is not recommended to forcefully respond with html template for a json request or vice versa.
I hope you have enough reasons for this which are best known to you.
Anyways, if you want to forcefully process a particular request as html, you can append .html in the end of request url.
e.g. If you want to process index action of users controller whose url is
http://localhost:3000/users
To achieve it
Call controller action with url
http://localhost:3000/users.html, controller will treat request as html and it will respond with html(you will need to have a html template for your controller action).
Similarly if you want to respond json for any request, append .json to the request url,
http://localhost:3000/users.json
controller will treat request as json and it will respond with json(you will need to have a json template for your controller action).

Generate Text (.txt) file for Data displayed in GSP

I have a Grails app developed in 2.3.6
There's a GSP file with HTML and CSS elements in it, and that displays data in multiple tables with headers.
I want this data to be saved into a text file and save it. So basically what i want to do is, there will be a Export button in this GSP page, and when user clicks on it, it will download the text file with all the data from that GSP.
What i tried so far?
def textFile = {
response.setHeader('Content-Disposition', 'Attachment;Filename="textFile.txt"')
render view: 'textFile', contentType: 'text/plain'
}
The problem with above is, it saves not just data, but also HTML & CSS elements.
I don't want any HTML or CSS in the text file. Only data from GSP is needed.
Is there a simple way of doing it.
the answer is simple - you need another view withouth the html and css parts.
The rest of your code looks good. But Grails itself does not convert your view, it just sends the content type to the browser and the browser tries to diesplay the data according to the content type.
If you don't want to write a new view (in most cases, writing the new view is dead simple), you could write your own converter (something which strips the HTML and CSS from your file) by creating an afterView-Filter: http://grails.github.io/grails-doc/2.4.0/guide/single.html#filters
Hope that helps

JSON shown instead of HTML for rails view using ember.js

I'm slowly moving my rails website from a traditional rails round-trip-for-each-view application to one that will eventually be single page ember.js based. As part of this migration, I'm doing it in steps and not migrating the whole app in one go to a single page app but section by section at first.
I'm stumped on one problem that seems general. When I use the same controller for JSON views as well as HTML views, pressing back in Chrome occasionally shows me the JSON view instead of the HTML view.
For instance, I have an endpoint /portfolio/13, which is the entry point into one of these section ember.js apps and which causes Ember Data to pull the JSON for Portfolio with the id of #13 over the same endpoint with application/json as the Accept: header. Pressing back after forward navigating to deeper page will get the JSON representation of the page instead of the HTML.
Do I need to force Ember Data to use the format parameter such that the JSON version is at a different URL? If so, how does one do that?
What am I doing wrong?
Try adding this somewhere in your Javascript:
$.ajaxSetup({cache: false});
It should fix the problem. However, something is wrong here, because by default browsers don't cache JSON. Probably, this is because of the wrong content type, that is, Rails is serving JSON as HTML.
Are you having some headers set as "application/json" in your rails app? Could you trace the network response headers and see the content-type: value?
Is the "whole" page displaying as JSON? ( meaning not parsing HTML )
I had this problem once using sinatra and I had a ( very stupid ) :
before do
content_type 'application/json'
end
If you want your browser to display correctly, it has to be 'text/html' for all your HTML pages.
It might be your problem at some places in your rails app. ( But why haven't you had this problem before ember? )

Resources