How to setup docpad.coffee helper functions for collection generation? - docpad

For collections I'd like to use helpers defined within docpad.coffee such as
getLang: (inLang) ->
"/" + inLang + "/"
...
myCollection: ->
#getCollection("html").findAllLive().on "add", (model) ->
model.setMeta({ url: #getLang("en") + defaultUrlPartGoesHere })
but can't get the FilesCollection to know my helper :/
How to setup helper functions to become available for collection definition?

Referencing docpadConfig.templatedata.getLang() will work, but if you find that distasteful, remember that docpad.coffee is just a standard NodeJS module (written in coffeescript). You could also define your function outside of the docpadConfig literal object and then both pull it into your templateData (assuming you need it for templates) and use it when building your collection.
For example:
# define the function outside of the config object
getLang: (inLang) ->
"/" + inLang + "/"
docpadConfig = {
templateData:
getLang: getLang # reference the previously defined function
collections:
myCollection: ->
# use the previously defined function
#getCollection("html").findAllLive().on "add", (model) ->
model.setMeta({ url: getLang("en") + defaultUrlPartGoesHere })
}

Mh, not the (most) elegant way to go but using the absolute path within docpad.coffee I can reference it with e.g. docpadConfig.templateData.getLang(...) if defined within templateData.

Related

Terraform: Need to pass a bash script to 3 different Launch templates's userdata

I am trying to pass to 3 AWS launch template userdata a bash script. This script calls other scripts from Github depending on a specific variable. Since each launch template must call different scripts what is the best way to accomplish it. I am currently trying to configure a data source template_file but I canĀ“t find a way to do what I need.
This is a piece of the bash script where I put a variable that need to change its value depending on which launch template is being built every time:
#------------------------------------------------------------------------------------------
# Define here scripts (separated with 1 space) that will be executed on first run:
AMI_SCRIPTS="ami_base_lynis.sh ${ami_script}"
#------------------------------------------------------------------------------------------
download_and_run_scripts
This is the template file data source:
data "template_file" "AMIs"{
template = "${file("../AMIs/s1_aws_userdata.sh")}"
vars = {
ami = var.dci_appserver_ami
}
}
And this is the user data attribute:
user_data_base64 = base64encode(data.template_file.AMIs.rendered)
This is not working for me as it will replace the variable has the same value for all 3 launch templates. How can I assign each time a different value?
The syntax you used for user_data_base64 tells me that you're using Terraform v0.12 or later, so you should no longer use template_file as shown in the template_file documentation:
In Terraform 0.12 and later, the templatefile function offers a built-in mechanism for rendering a template from a file. Use that function instead, unless you are using Terraform 0.11 or earlier.
Because of that, I'm going to answer using the templatefile function instead.
Inside each of your launch template resource blocks, you can call templatefile with different values for the template variables in order to get a different result each time:
resource "aws_launch_template" "example1" {
# ...
user_data = base64encode(templatefile("${path.module}/../AMIs/s1_aws_userdata.sh", {
ami = var.dci_appserver_ami
ami_script = "script1.sh"
}))
}
resource "aws_launch_template" "example2" {
# ...
user_data = base64encode(templatefile("${path.module}/../AMIs/s1_aws_userdata.sh", {
ami = var.dci_appserver_ami
ami_script = "script2.sh"
}))
}
resource "aws_launch_template" "example3" {
# ...
user_data = base64encode(templatefile("${path.module}/../AMIs/s1_aws_userdata.sh", {
ami = var.dci_appserver_ami
ami_script = "script3.sh"
}))
}
You could in principle factor out constructing the templates into a local value if you want to do it more systematically, but since your question didn't include any indication that you are doing anything special with the launch templates here I've just written the simplest possible approach, where each launch template has its own template-rendering expression.

Terraform lambda_function_association for CloudFront as optional list within a module

we have our static stack (CloudFront, S3, ..) defined as a configurable module for different projects. Now some of them need edge lambdas and I wanted to make them configurable (and optional(!)), too.
We are using the module as following:
module "static" {
..
lambda_function_associations = [
{
event_type = "viewer-request"
lambda_arn = "${aws_lambda_function.onex_lambda_viewer_req.qualified_arn}"
},
{
event_type = "viewer-response"
lambda_arn = "${aws_lambda_function.onex_lambda_viewer_res.qualified_arn}"
},
]
..
}
and the default cache behaviour of CloudFront is defined as the following:
default_cache_behavior {
..
lambda_function_association = ["${var.lambda_function_associations}"]
..
}
and our variable within the module:
variable "lambda_function_associations" {
type = "list"
default = []
}
Applying this stack I get:
Error: module.static.aws_cloudfront_distribution.web: "default_cache_behavior.0.lambda_function_association.0.event_type": required field is not set
Error: module.static.aws_cloudfront_distribution.web: "default_cache_behavior.0.lambda_function_association.0.lambda_arn": required field is not set
Is there no way to make them work optionally? I really dont want to duplicate the whole stack when adding an edge lambda.
Apparently something like this works for lb_health_check configuration blocks:
https://github.com/hashicorp/terraform/issues/17292#issuecomment-393984861
Thanks in advance!
I recently stumbled upon the same issue. This is caused by a terraform limitation, which prevents us from passing dynamic values to a nested block inside a module.
The only workaround I found was duplicating the resource declaration and creating one of the resources based on a condition in the count variable (pass a static variable here, e.g. associate_lambda_function).
You can find more details and an example in this gitlab snippet

Call one function from another using codeigniter

I'm using CodeIgniter and I came across an interesting problem. I need to use the variables from one function on another. I was planning to do this by simply declaring global variables (which I wasn't able to) do in the framework. So I tried calling one function from within another (this is all happening in the controller). Since apparently this can't be done I made a helper file with the common function and then just tried to load it but I get this error:
Fatal error: Call to undefined method ReporteNominas::getValues()
the helper file is inside the helpers folder and it contains this:
function getValues($getThem, $tpar, $vpiso, $tcomi, $tgas, $ttotal){
$totalPares = $tpar;
$ventasPiso = $vpiso;
$totalComisiones = $tcomi;
$totalGastos = $tgas;
$totalTotal = $ttotal;
if($getThem){
return $totalPares . "," . $ventasPiso . "," . $totalComisiones . "," . $totalGastos . "," . $totalTotal;
}
}
and I'm trying to call it doing this:
$this->load->helper('helper_common_functions_helper');
$this->getValues(false, $query['cant'], $query['sum'], $query['com'], $query['gas'], $query['tot']);
what could I be missing here?
Try this:
$this->load->helper('helper_common_functions_helper');
getValues(false, $query['cant'], $query['sum'], $query['com'], $query['gas'], $query['tot']);
A helper (if done properly) is just a group of functions, NOT a class, so you can call it as a regular function call.
You should also do this, in your helper:
if ( ! function_exists('get_values'))
{
function getValues($getThem, $tpar, $vpiso, $tcomi, $tgas, $ttotal)
{
//rest of code
}
}
To avoid a 'redeclare function' error when loaded more than once
Helper's are just functions so instead of calling them like a class with $this-> you just call them like a normal php function. So change this
$this->getValues(false, $query['cant'], $query['sum'], $query['com'], $query['gas'],$query['tot']);
to this
getValues(false, $query['cant'], $query['sum'], $query['com'], $query['gas'],$query['tot']);

jquery plugin: run code in variable?

I am making a jquery plugin.
I am trying to create a jquery plugin,
I am attempting to add the ability for users the run a function at certian times.
so, in the option array the user could have
{created: function(){alert('created called!');}}
Now in the plugin,
How do i run that code?
i tried just
options.created and eval(options.created);
but neither have any effect.
What am i doing wrong?
Have you tried options.created()?
More info: When you set a variable to an anonymous function in JavaScript, you must call the variable the same way you would any "regular" function. This allows you to pass parameters to the function.
For example:
var aFunction = function(a, b) { alert(a + " " + b); };
aFunction('Hello', 'world');

Prototype: how to dynamically construct selector?

I am having a little bit of difficulty passing a variable into a selector in prototype. I would like to be able to pass a variable into the select string, so that one function can work for many of the same kind.
At the moment, this is what I would basically like to do:
function myFunct(var)
{
$(var + 'add_form').hide() //so inde the brackets would be ('#product1 #add_form') for example.
}
Be able to pass 'var' into the function that would pass it to the selector, so that I can hide a pattern that is the same for many on the page.
Any ideas for a path to follow would be greatly appreciated.
You're on the right track! Couple things:
var is a JavaScript keyword (source), don't use it to name a variable
if you're querying an element by id (such as #add_form) you don't need to add any container element as you're doing
If you're querying an element by class, you need to use the $$ function, not the $ function
You need to iterate over the wrapped set to call your method
whitespace is significant in css selectors, so make sure to include those in your selector construction to tell Prototype to search within your parent container:
function myFunct(parent) {
$$(parent + ' .add_form').invoke('hide')
}
myFunct('#someparent'); // hides .add_form inside #someparent
That should work... just rename var to something else.
function myFunct(yourVar)
{
$$('#' + yourVar + ' .add_form').each(function(s){ s.hide(); }); // yourVar being the id of the container element
}
I've put a '.' in front of add_form because you can't use multiple elements with same ID, make it a class.

Resources