Hot answers tagged

26

I just threw together a add-on that will provide a Twig filter called prune to allow you to more easily do what you're asking here. https://github.com/mattstauffer/craftcms-prune With prune you can now just do the following: {{ craft.entries.section('news').find() | prune(['title', 'body']) | json_encode() | raw }} Just pass in an array of the fields you ...


24

Twig is great for outputting HTML/XML markup, but it’s not exactly the best option when you just want to output some data as JSON. The Element API plugin aims to make outputting entries (and other element types) as JSON as easy as possible, and it’s completely customizable from its PHP-based API configuration file. To achieve your desired JSON output, you ...


9

You could also loop through all fields and output it depending on the field-type. Example: {% spaceless %} [ {% for entry in craft.entries.section('news').limit(1) %} { {# General Fields #} 'slug': {{ entry.slug | json_encode() | raw }}, 'postDate': {{ entry.postDate | json_encode() | raw }}, 'title': {{ entry.title ...


9

You certainly can using the Redactor Formatting config. Locate the config file for the Redactor setting you want to edit in craft/config/redactor and add this line: "formatting": ["h1", "h2", "h3"], Or any combination of elements you want to be made available. For those reading this who can't see the Formatting option for their Redactor field, it's also ...


8

Just remove these two logging routes from your plugin's init() method in your PluginHandlePlugin.php file: craft()->log->removeRoute('WebLogRoute'); craft()->log->removeRoute('ProfileLogRoute'); That will prevent the extra logging and profiling information to be output in the browser's console when devMode is enabled.


8

It turns out that the latest redactor seems to now have a plugin for viewing the source. You can enable this in your redactor JSON config file under plugins. Just add the "source" plugin like so: { "buttons": ["format", "bold", "italic", "lists", "image"], "plugins":["fullscreen", "source"], "toolbarFixed": true }


8

What I like to do is to override the default options of the Format dropdown by setting "formatting" to null and adding my own list of options using "formattingAdd". This allows you to come up with your own names for the options. { "buttons": ["format","bold","italic","link"], "formatting": null, "formattingAdd": { "h3": { title: 'Heading', ...


8

Something like this in a .json file should work: { "HTML.SafeIframe": true, "URI.SafeIframeRegexp": "%^(https?:)?//(www\\.youtube(?:-nocookie)?\\.com/embed/)%", "Attr.AllowedFrameTargets": ["_blank"], "HTML.AllowedComments": ["pagebreak"] } Note that the first two lines were ...


7

You can fetch GET parameters using HttpRequestService::getQuery() or getRequiredQuery(): 'entries-by-category' => function() { $categoryIds = craft()->request->getRequiredQuery('categoryIds'); return [ 'elementType' => 'Entry', 'criteria' => [ 'relatedTo' => ['targetElement' => $categoryIds], ...


6

The $entry variable that is passed to your endpoint’s transformer function will be an EntryModel object - the same type of object you use when interacting with entries in your templates. So everything you can do in your templates you can also do from this function. The easiest way to get a Structure section entry’s children is with the getChildren() ...


6

$entry->postDate will give you the postDate property of the EntryModel, which itself is a DateTime object. The JSON transformer will do its best to convert that object to JSON by returning its visible properties, which produces the JSON object in your example. Angular has built-in date filters, but it requires the input data to be in a recognized format....


5

Check out the FeedMe plugin. It's pretty similar to DataGrab and Importer. From the README: Feed Me Feed Me is a Craft plugin to make it easy to import entries and entry data from XML, RSS, ATOM or JSON feeds. Feeds can be setup as a task in Craft's Control Panel, or called on-demand for use in your twig templates. A common use-case for this ...


5

I am just starting out on a project using both Craft and Angular. I ran into an early issue because both Angular and Twig use the {{ }} braces, but that was quickly fixed by changing Angular’s settings like in this Stackover flow post. As Christian noted in the comments, though, you could also use the {% verbatim %} tag to keep Craft from parsing Angular ...


5

You could also do what Victor In has suggested, but deal with everything in arrays and hashes instead of building a big string. That way you only have to encode once at the end, and it's easier (for me) to debug. {% spaceless %} {% set entries = [] %} {% for entry in craft.entries.section('people') %} {% set thisEntry = {} %} {% for fieldModel ...


5

You need to add "html" in the buttons array, and not "source" in the plugins array.


5

Something like the following should work: // elementapi.php return [ 'endpoints' => [ 'assets.json' => [ 'paginate' => false, 'elementType' => ElementType::Asset, 'criteria' => [ 'limit' => null, ], 'transformer' => function(AssetFileModel $asset) { ...


5

Seems like a simple striptags filter would do nicely in this case: <h1>{{ entry.titleField|striptags('<strong><em><br>')|raw }}</h1> If you're having issues with linebreaks, you might want to add the nl2br filter as well: <h1>{{ entry.titleField|striptags('<strong><em><br>')|raw|nl2br }}</h1>


5

The answer is to create a custom plugin that will read the json files from the folders in the public directory, parse the json, and pull them into the template. An alternative solution is to use Craft custom config files. (Not the answer you're looking for but would work very well.) In craft/config create a file like localedata.php and make it something ...


4

I had the same problem. See Brad's response here to so this without messing with devMode: Console script tags garbling up non-html output (e.g. json) when in devMode It would be nice if this happened automatically when specifically outputting non-html, though.


4

You already mentioned the related answer in custom fieldtype with multiple values, and it actually is perfect for your usecase. It doesn't really matter how you transport the data over to Craft, because if the content is set to AttributeType::Mixed, then Craft will JSON encode/decode it when requested. So, to save your data, send over an array, like so: $...


4

This is most likely possible by serving up restful resources that refer to Craft data with some sort of PHP API that you'll need to write. The APIs you write will just need to grab data using Craft's API As for hiding certain content for mobile (or whatever frontend platform), I'd create a Lightswitch field for section that you can use to signify content ...


4

You're likely missing the PHP JSON extension, which is a requirement to run Craft.


4

You could write some PHP at the top of your general.php file that reads the JSON manifest in your public root directory, and add its keys/values to the config array. I wouldn't use environment variables here, personally – just add the manifest to the returned array in general.php: // In general.php: $assetsManifest = @json_decode(file_get_contents($...


3

You can set the header in Twig with the {% header %} function. Like this: {% header "Content-Type: application/json; charset=utf-8" %} {% header "Access-Control-Allow-Origin: *" %} As to generating the actual json, my favourite is using the prune plugin and do it like this: {% set myEntries = craft.entries.section('mySection').find() %} { {%- ...


3

See here for various methods of setting the response code depending on what version of PHP and which PHP module you're running.


3

Completely untested, but a possibility would be to define your content attribute like so: return array(AttributeType::Mixed, 'model' => 'MyPluginContentModel'); MyPluginContentModel would have every possible value that you would want to expose to your template ('id', 'obj', 'etc') defined as an attribute. When Craft pulls the JSON from the database, it ...


3

The Import plugin by Bob Olde Hampsink can import CSVs. JSON support is on the roadmap, but in the meantime, it’s easy enough to convert JSON to a CSV with a little bit of Javascript. Just paste this into your browser’s JS console on any webpage: var data = [ { title: "Entry Title", body: "Body content" }, { title: "Entry Title 2", body: "More body ...


3

You could try setting PHPUnit's @runInSeparateProcess flag, as discussed here. However, it sounds as though you're attempting to use PHPUnit for acceptance or integration tests, when it's really designed to be used for unit testing. A couple of options: You could mock the JsonHelper::sendJsonHeaders method, but that may prove tricky; You could use ...


Only top voted, non community-wiki answers of a minimum length are eligible