I have written following template in my chef cookbook recipe
template '/etc/app.conf' do
variables({
my_id: Chef::HTTP.new(https://example.com).get('/',{header})
})
end
And my erb file is
Output is : <%= #my_id %>
I actually want to perform some ruby operations(mainly filter out and count the components of my_id) and then pass those values(count of each component) back to the template and use it further. What should be erb configuration or anything thats need to be added in template block?
(Here, my_id actually has the subnets and I want to get those the count of those subnets and its values so that I can use it further to perform another http request and get the nodes in each of the subnet).
Don't know much about Chef Cookbooks but you can write some ruby within an ERB template. It is not the cleanest solution I believe though.
See here on how to embed code in your ERB
The thing you pasted from (hopefully you were summarizing since you missed a bunch of quotes in there) was only a quick tip. To get JSON data you want to use Chef::HTTP::SimpleJSON, the will do the parsing for you and whatnot.
variables data: Chef::HTTP::SimpleJSON.new('https://whatever.com/').get('/foo')
Related
By running a method I would like to fetch the output of a compiled erb-template and store it in a variable instead of rendering it.
I would like to be able to use rails' helper functions (e.g. distance_of_time_in_words_to_now or including partials) in the template.
How would I go about that?
Thank you!
I would like to write files like:
ideas.md
social-pomodoro.md
ynm.md
And not have to write titles, eg:
---
title: Ideas
---
But titles should be generated from filenames using something similar to Rails #humanize method.
As this isn't a built-in functionality of Jekyll (at time of writing), I suppose you have 2 options:
Write a your post, and then use the {{ post.path }} tag and manipulate the path to get the filename and save that as the post title.
This would however require you to write frontmatter and other post details. Here's a related SO question.
(recommended) Use a script to generate the filename based on the title.
This is similar but a reverse of what you wanted, in which a script would take in parameters such as post title and post tags, and generate a file with the correct Jekyll post filename (name-of-post-2015-01-30) (I think Jekyll posts need dates to avoid collisions). The script will also generate the appropriate YAML front matter so you can get writing right away and not deal with repetitive overhead. All you would have to do it something like ./post.sh "Title" "optional_tags and you're good to go!
Here is a Python script written by Josh Branchadu that creates the file with details like dates and filenames all taken care of. There's also a similar bash version as well. Lastly a a script that uses the Thor Ruby in automating Jekyll posts that should let you create a post in the most painless way possible.
I have created the ability for users in my system to edit a liquid template that is eventually rendered and turned into a PDF. I would like some ideas as to what the best method would be to create some mock objects to feed the template so as to create a preview for them to see what the final result of their template modifications will be.
The collection of objects passed to the template when it is rendered in real life is fairly complex so I'm thinking at this stage that I can either try and build a temporary model with dependencies in memory, or create some structs that pretend to be the models in question and pass those to the template instead.
Another way could be to instantiate all of this from a yaml file.
Any ideas welcome :)
If you trying to create objects, why dont you use a factory? Are the objects part of the database? You could always use seeds.rb to seed the database with some demo data.
I ended up using a YAML file to build up the structure I needed. It seems that liquid will take a hash of values (and other hashes) instead of the actual models with relationships no problem, so I didn't even need to instantiate the models.
Will happily post an example if anyone is interested.
When doing J2EE development, I find it handy for debugging to view the Java classes that are generated by the JSP compiler.
How can I do the equivalent in Ruby? Since it is all in memory, it won't generate a file that I can view. I believe it's the ERB module that generates the corresponding object for a template, so how can I actually view the object? Can I drop a debugger statement somewhere and use rdb? Is there some configuration value I can tell it to dump the object definition? I'm using rails, in case that makes a difference.
I don't think rails generates a class for your view. It basically calls eval after processing the file. Or do you mean inspecting the erb object while it's parsing your template?
If it's the latter you can find erb.rb in lib\ruby\1.9.1 I'd imagine you could just drop a debugger statement throughout that file.
I always make a habit of adding the following to my views (layout) which allows me to inspect or debug the parameters being used by the view in question.
<%= debug(params) %>
This will format all the parameters in yaml and display them in a Hash format.
Have a look at the method in the source code to get a better understanding. SOURCE
There are some differences compared with the Java way due to language differences.
Most template libraries for Ruby follow these steps when compiling/optimizing:
The template is compiled into Ruby source code -- not a class but a long procedure that appends to a string buffer while traversing the logic of the original template.
This ruby code is evaluated in order to be bound for later reference, preferably within a method body. This way, it is only parsed once by the interpreter.
The method (or other context) containing the logic of the parsed template is invoked to render it.
Anyway, the compiled template code therefore looks a lot like a much noisier version of your original template, and will generally not help you debugging, unless you're debugging the template language itself.
Anyone interested in template language implementation might enjoy a look around the Tilt code (use different template languages with the same rendering interface and optimization), and Temple (a great template language meta-implementation).
Is there currently a plugin that you ruby on rails developers that are also using macvim/gvim/vim that allows you to take a quick block of code and create a partial from it? I know that TextMate does this, figured someone has ported it by now to vim also.
You want Tim Pope's rails.vim plugin:
http://rails.vim.tpope.net/
It provides an :Rextract command that pulls a range of lines into a partial. Here's a very short demo of it in action:
http://rails.vim.tpope.net/images/rpartial.gif
(The :Rpartial command in the demo is an alias for :Rextract.)
The plugin provides dozens of other features, too, and many people consider it a must-have for Rails development in Vim.
It works in vim with vim-rails plugin.
Select in visual mode code which you need to send to partial
Press key : and you will see :'<,'>
Complete command to :'<,'>Rextract partial_name where partial_name will your partial's file name. You can set folder for partial, for example :'<,'>Rextract shared/menu
Press Enter and enjoy.
rails.vim can do this. From the features summary:
:Rextract file replaces the desired
range (ideally selected in visual line
mode) with render :partial => 'file',
which is automatically created with
your content. The #file instance
variable is replaced with the file
local variable.