I'm trying to figure out a way to take a erb template file and use hiera data (even from a single yaml file) and just use the template to generate a file with replaced values.
Have you ever tried anything like this? My first thought is to write a ruby script, but maybe there's a simpler solution.
Thanks in advance.
Edit:
Since it might not be clear, let me explain the use case.
I want all application configuration to be templated and committed by developers and I want to provide them an automated way of filling those templates in on their local machines (laptops) without using puppet. The extra benefit is validation of templates before actually committing them.
I'm not sure why you would want to do this from hiera data directly, but it would be easy to implement with the ERB and yaml ruby libs. Something like (psudo-code):
class Erbwritter
require 'erb'
require 'yaml
attr_accessor :output_path, :yaml_path
def initialize(template, command)
#output = :output_path
#data = :yaml_path
.....
end
def render()
ERB.new(#template).result(binding)
end
def save(file)
File.open(file, "w+") do |f|
f.write(render)
end
end
def parse_yaml(#data)
File.open(#data, ...
# parse some stuff, add them to a local {}
end
end
Then, you can instance this class like:
newTemplate = Erbwritter.new(/path/to/output, /path/to/yaml)
newTemplate.save(File.join(Dir.pwd, your_file_name"))
Again, this is all basically psudo code and won't work out of the box, but it's pretty close. So have fun.
You can read more about ERB Class here.
Related
new to chef and trying to pass a long list of variables to a batch file run. Have thought of writing a list of these in a template and calling the template in the recipe i.e:
options = template "optionstemplate"
How does one set a variable using hashes from a template of in chef? Please do let me know if this is possible of if there is another way to do this.
While technically doable, this is very hard and not recommended. You should instead write some Ruby code to generate the options string you need. Maybe something like:
options = ''
node['mythingy']['options'].each do |key, value|
options << " --#{key}=#{value}"
end
I’m new to Crystal (and never really used ruby) so apologies for the ignorance here! I've looked at the ecr docs but can't seem to find an answer there.
I’m looking at using Embedded Crystal for dynamic templates in Kemal. Can I confirm - can templates only render variables that are available in the scope of the call, or can one make method/function calls from within the template itself? I.E. is there any possibility/risk of being able to execute “malicious” crystal code from within a template (in this case malicious refers to I/O or file access etc)?
To take an example from the Kemal docs:
get "/:name" do |env|
name = env.params.url["name"]
render "src/views/hello.ecr"
end
In the view hello.ecr - is name the only item that will be available in the template, or could one call File.delete("./foo")from within the template for example?
A template is compiled into Crystal code, you can write any kind of code in there, like File.delete("./foo"), for example if you write <% File.delete("./foo") %> inside of your template.
If your worry is that name will contain code and that will somehow get executed, then don't worry, that's not going to happen. Dynamic runtime code execution in Crystal is not possible, so there's no way someone will inject malicious code into your templates.
This might be a stupid question, but I am new to coding with Python.
Using flask and calling #app.route(), i need to create several HTML files.
Rather then coding everything inside of #app.route(), can i call different def inside the app.route before returning the render_template?
Edit:
So i am creating HTML files. Rather then opening 3-4 different documents inside the app.route and printing lines to them, can I create 3-4 functions in the main code to handle each document.
so rather then:
#app.route('/')
Print all html files
have:
def html1():
write html files
#app.route('/')
html1()
render_template
Yes, you can do whatever processing you need to inside a route before finally returning the response to the client. Be aware of load times, however - people don't like waiting long for a page to load.
That being said, you should really take a look at the template tutorial of the official docs. Your approach sounds like a perfect place to make use of templates as Jinja is doing what you describe, but in real-time: taking the HTML file, replacing certain places with your data, and presenting that to the user.
I make an application writed in Rails, it's growing fast and I learning with it. But I'm don't understand about helpers.
application_helper.rb
module ApplicationHelper
# This file it's empty
end
users_helper.rb
module UsersHelper
def avatar
# Do something
end
end
customer_helper.rb
module CustomerHelper
# This file it's empty
end
Why in any customer's view can call avatar helper method on user helper module?
Then, why separate helpers in many files?
Thanks in advance.
P.S: Rails' version 4.
Because all helpers are included in all controllers, by default. The separate files are really just for logical separation in this scenario. You can change that behaviour though:
By default, each controller will include all helpers.
In previous versions of Rails the controller will include a helper
whose name matches that of the controller, e.g., MyController will
automatically include MyHelper. To return old behavior set
config.action_controller.include_all_helpers to false.
http://api.rubyonrails.org/classes/ActionController/Helpers.html
To add to Mike Campbell's answer:
Framework
As magic as it is, Rails is a set of files which are called
sequentially
These files hold classes, methods, etc; but they're still files.
And that means that when you run an action through Rails, it loads up
a series of other dependent files to help it run (that's what a
framework is)
The design of Rails is such that your helper methods are all
loaded each time you run an action. I don't know why, but it helps
administer the methods for different areas of your app
So to answer your question, there's no real reason why the helpers are split up, at least with Rails 4
I see I can easily modify the Meta options of a Serializer at run time (i'm not even sure this is the right way to call it, I read around somebody call it monkey patching, even though i don't like it):
NodeDetailSerializer.Meta.fields.append('somefield')
What if I need to do something like:
NodeDetailSerializer.contact = serializers.HyperlinkedIdentityField(view_name='api_node_contact', slug_field='slug')
NodeDetailSerializer.Meta.fields.append('contact')
Why would I need to do that?
I'm trying to build a modular application, I have some optional apps that can be added in an they automatically add some features to the core ones.
I would like to keep the code of the two apps separate, also because the additional applications might be moved in a different repository.
Writing modular and extensible apps is really a tricky business.
Would like to know more about that if anybody has some useful resources to share.
Federico
I found a solution for my problem.
My problem was: I needed to be able to add hyperlinks to other resources without editing the code of a core app. I needed to do it from the code of the additional module.
I wrote this serializer mixin: https://gist.github.com/nemesisdesign/8132696
Which can be used this way:
from myapp.serializers import MyExtensibleSerializer
MyExtensibleSerializer.add_relationship(**{
'name': 'key_name',
'view_name': 'view_name_in_urls_py',
'lookup_field': 'arg_passed_to_to_view_name'
})