Joomla Component override template - view

I'm writing a Joomla 2.5 or 3.x component
which make data elaboration and returns some data.
I want that work as a service that is invoked and returns data
(eg
I call www.mysite.com?option=com_mycomponent&view=myview
and result for example my json data ..or xml or what i need after my elaboration
)
so i need that my output view is raw.
i need no template and no css or js..
only my result..
but now the results are inserted into the template
Is it possible?...
I tried to create a RAW mode in my template
like Here .
this works but is not what I want
but it is a dirty solution
because it work if the url i have to call is like ...
www.mysite.com~....~&tmpl=raw
I'd like my component can output as raw.
Thanks

Create RAW view views/[myview]/view.raw.php inside your component
In requests require RAW format
index.php?option=com_mycomponent&view=myview&format=raw.
Like in com_banners/views/tracks/view.raw.php.
Sames goes for JSON and XML.
Here's a list of generic document formats: libraries/joomla/document
feed
html
image
json
opensearch
raw
xml
To use JSON format in response, I recommend new JResponseJson class:
// Anything that may be serialized with json_encode or an Exception
$data = array('some' => 'data');
echo new JResponseJson($data);

Related

How to display array, json directly in freemarker template?

Problem arises when data coming to template is not fixed in nature, for example we were trying to show headers of an API call snippet of which looks like this:
"pragma":"no-cache","via":"1.1 vegur","access-control-allow-credentials":"true","CF-Cache-Status":"HIT","x-content-type-options":"nosniff","NEL":{"success_fraction":0,"report_to":"cf-nel","max_age":604800},"Report-To":{"endpoints":[{"url":"https://url?s="}]

How to get a HTTPRequest JSON response without using any kind of template?

I am new to Django but i am advanced programmer in other frameworks.
What i intend to do:
Press a form button, triggering Javascript that fires a Ajax request which is processed by a Django View (creates a file) that return plain simple JSON data (the name of the file) - and that is appended as a link to a DOM-Element named 'downloads'.
What i achieved so far instead:
Press the button, triggering js that fires a ajax request which is process by a Django view (creates a file) that return the whole page appended as a duplicate to the DOM-Element named 'downloads' (instead of simple JSON data).
here is the extracted code from the corresponding Django view:
context = {
'filename': filename
}
data['filename'] = render_to_string(current_app+'/json_download_link.html', context)
return HttpResponse(json.dumps(data), content_type="application/json")
I tried several variants (like https://stackoverflow.com/a/2428119/850547), with and without RequestContext object; different rendering strats.. i am out of ideas now..
It seems to me that there is NO possibility to make ajax requests without using a template in the response.. :-/ (but i hope i am wrong)
But even then: Why is Django return the main template (with full DOM) that i have NOT passed to the context...
I just want JSON data - not more!
I hope my problem is understandable... if you need more informations let me know and i will add them.
EDIT:
for the upcoming questions - json_download_link.html looks like this:
Download
But i don't even want to use that!
corresponding jquery:
$.post(url, form_data)
.done(function(result){
$('#downloads').append(' Download CSV')
})
I don't understand your question. Of course you can make an Ajax request without using a template. If you don't want to use a template, don't use a template. If you just want to return JSON, then do that.
Without having any details of what's going wrong, I would imagine that your Ajax request is not hitting the view you think it is, but is going to the original full-page view. Try adding some logging in the view to see what's going on.
There is no need to return the full template. You can return parts of template and render/append them at the frontend.
A template can be as small as you want. For example this is a template:
name.html
<p>My name is {{name}}</p>
You can return only this template with json.dumps() and append it on the front end.
What is your json_download_link.html?
assuming example.csv is string
data ={}
data['filename'] = u'example.csv'
return HttpResponse(simplejson.dumps(data), content_type="application/json")
Is this what you are looking for?

reading data from Ruby in D3 (JSON)

i'm confused how to read data from ruby class.
assume i have a class, called "Points".
what i want is reading the all data from this 'Points'.
what i did is like this :
var allPoints = <%= Point.all.to_json %>;
d3.json('allPoints',
function(data){ .......}
i dont know why but somehow d3 can't read the varable allPoints.
is there maybe something i forget to put in code?
or maybe there's another way to read all data from Ruby?
d3.json() is used for performing an AJAX call and retreiving JSON data from a server, which is then passed to the provided callback. It appears that what you're doing is generating a page with your data embedded in the page as a JavaScript. To use this object, just use it. Use whatever code was in your callback function, replacing the name data with allPoints.

How do you use enum inside switch statement using MVC & razor view engine?

I have a service which returns a Json data structure. One of the properties in the data is an enum value. When the Json is returned inside the client, I'd like it to react to the data returned using a switch statement.
I don't want to hard-code the enum values in the javascript, so I tried using case #MyEnum.Value, but the view renders it simply as 'Value'. I need it to output the actual value of the enum for it to work.
How can I get this to work, or alternatively is there a better method for handling this situation?
Try #((int)MyEnum.Value) for getting the value

Any alternate of drupal_json for non-JSON output

In an AJAX call back in drupal it is normally recommended to use drupal_json() to send data to client. This function converts the raw data into JSON along with HTML encoding.
I want to send the HTML data without encoding to client.
for this I am using following code:
print $html_output;
exit(0);
Is there any recommended or best way in drupal to do so?
If you need to output only the HTML output returned from the menu callback, then the following code is the correct one:
print $html_output;
module_invoke_all('exit');
exit();
If you want your output to appear together the blocks Drupal normally output, then the code needs to be changed to the following:
return $html_output;
That will do the trick. Allthough you should invoke hook_exit first. However this is shortcutting the framework some what, it may work in simple cases but wont work for forms etc.
The only time I have used this method is if I am printing some data which is allready json encoded.

Resources