Having a bit of an issue generating channel fields based on a variable. Below is my attempt at a PHP solution:
{related_entries id="image_gallery"}
<?php for ($i = 1; $i <= 15; $i++) { ?>
{image_<?php echo $i; ?>}
<?php } ?>
{/related_entries}
Unfortunately ExpresssionEngine doesn't render these tags and just outputs them as:
{image_1} {image_2} {image_3}... etc.
I've tried some other looping plugins with no luck either, but I assume there is a plugin or method out there to help me.
Bonus question: Ideally, I will need a method that I can also check to see if the channel field actually has any data like: {if image_x}{image_x}{/if}
Make sure you have set PHP to parse on input in your template preferences. That should do the trick.
Related
I am having a perl script in which i am giving path to directory as input.
Directory has xml files inside it.
In my code i am iterating through all the xml files and creating absolute path for all xml files. Code is working fine.
#!/usr/bin/perl
use File::Spec;
$num_args = $#ARGV + 1;
if ($num_args != 1) {
print "\nUsage: $0 <input directory>\n";
exit;
}
my $dirPath = $ARGV[0];
opendir(DIR, $dirPath);
my #docs = grep(/\.xml$/,readdir(DIR));
foreach my $file (#docs)
{
my $abs_path = join("",$dir,$file);
print "absolute path is $abs_path";
}
Question i have here is,
joining $dirPath and $file with no separator which means that $dirPath must end in a "/". So is there any way or built in function in perl which take cares of this condition and replaces the join method.
All i want is not to worry about the separator "/". Even if script is called with path as "/test/dir_to_process" or "/test/dir_to_process/", i should be able to produce the correct absolute path to all xml files present without worrying about the separator.
Let me know if anyone has any suggestions.
Please take heed of the advice you are given. It is ridiculous to keep asking questions when comments and answers to previous posts are being ignored.
You must always use strict and use warnings at the top of every Perl program you write, and declare every variable using my. It isn't hard to do, and you will be reprimanded if you post code that doesn't have these measures in place.
You use the File::Spec module in your program but never make use of it. It is often easier to use File::Spec::Functions instead, which exports the methods provided by File::Spec so that there is no need to use the object-oriented call style.
catfile will correctly join a file (or directory) name to a path, doing the right thing if path separators are incorrect. This rewrite of your program works fine.
#!/usr/bin/perl
use strict;
use warnings;
use File::Spec::Functions 'catfile';
if (#ARGV != 1) {
print "\nUsage: $0 <input directory>\n";
exit;
}
my ($dir_path) = #ARGV;
my $xml_pattern = catfile($dir_path, '*.xml');
while ( my $xml_file = glob($xml_pattern) ) {
print "Absolute path is $xml_file\n";
}
The answer is in the documentation for File::Spec, e.g., catfile:
$path = File::Spec->catfile( #directories, $filename );
or catpath:
$full_path = File::Spec->catpath( $volume, $directory, $file );
This will add the trailing slash if not there:
$dirPath =~ s!/*$!/!;
I am moving aspx files from an old system to the new one using Powershell. I need to parse the page and change href of hyperlink tags as following.
old system
href=/ranet/templates/page____9372.aspx
will be in new system
/newfolder/folder1/9372.aspx
Try this
$input = "href=/ranet/templates/page____9372.aspx"
$new = "/newfolder/folder1/"
$array = $input.Split('_')
$array2 = ($array[$array.Count-1]).Split('.')
$newLine = $new+$array2[0]+".aspx"
$input
$newLine
Actually this is not such a great answer as you have to ensure that there are no other underline characters in the page!
Is there a simple way to renumber pages in a DJVU file?
Example:
I've got a book, and page 1 is actually the cover, and so on, such that the actual page 1 of the book is at, say, 10 in the document; what I'd like to do is call them something like C,i,ii,..., and then 1,2,...
I know it can be done, since I've got other books in this format with this numbering, and I'd like to do it on Linux, better if via terminal.
Thanks,
N
to renumber
for (( i=11; i<=823; i++ ))
do
djvused new.djvu -e "select $i; set-page-title $((i-10)); save"
done
to rename
djvused new.djvu -e 'select 2; set-page-title ii; save'
It's slightly offtopic. Just in case someone needs to do the same thing on Windows using PowerShell:
for($i=11; $i -le 823; $i++){
$j=($i-10)
$args = "new.djvu -e ""select $i; set-page-title $j; save"""
write-host "djvused $args"
start-process djvused $args -NoNewWindow -wait
}
I'm trying to generate dynamic var names in a shell script to process a set of files with distinct names in a loop as follows:
#!/bin/bash
SAMPLE1='1-first.with.custom.name'
SAMPLE2='2-second.with.custom.name'
for (( i = 1; i <= 2; i++ ))
do
echo SAMPLE{$i}
done
I would expect the output:
1-first.with.custom.name
2-second.with.custom.name
but i got:
SAMPLE{1}
SAMPLE{2}
Is it possible generate var names in the fly?
You need to utilize Variable Indirection:
SAMPLE1='1-first.with.custom.name'
SAMPLE2='2-second.with.custom.name'
for (( i = 1; i <= 2; i++ ))
do
var="SAMPLE$i"
echo ${!var}
done
From the Bash man page, under 'Parameter Expansion':
"If the first character of parameter is an exclamation point (!), a
level of variable indirection is introduced. Bash uses the value of
the variable formed from the rest of parameter as the name of the
variable; this variable is then expanded and that value is used in the
rest of the substitution, rather than the value of parameter itself.
This is known as indirect expansion."
The Problem
You're using the value of i as if it were an array index. It isn't, because SAMPLE1 and SAMPLE2 are separate variables, not an array.
In addition, when calling echo SAMPLE{$i} you are only appending the value of i to the word "SAMPLE." The only variable you are dereferencing in this statement is $i, which is why you got the results you did.
Ways to Address the Problem
There are two main ways to address this:
Multi-stage dereferencing of an interpolated variable, via the eval builtin or indirect variable expansion.
Iterating over an array, or using i as an index into an array.
Dereferencing with eval
The easiest thing to do in this situation is to use eval:
SAMPLE1='1-first.with.custom.name'
SAMPLE2='2-second.with.custom.name'
for (( i = 1; i <= 2; i++ )); do
eval echo \$SAMPLE${i}
done
This will append the value of i to the end of the variable, and then reprocess the resulting line, expanding the interpolated variable name (e.g. SAMPLE1 or SAMPLE2).
Dereferencing with Indirect Variables
The accepted answer for this question is:
SAMPLE1='1-first.with.custom.name'
SAMPLE2='2-second.with.custom.name'
for (( i = 1; i <= 2; i++ ))
do
var="SAMPLE$i"
echo ${!var}
done
This is technically a three-step process. First, it assigns an interpolated variable name to var, then dereferences the variable name stored in var, and finally expands the result. It looks a little cleaner, and some people are more comfortable with this syntax than with eval, but the result is largely the same.
Iterating Over an Array
You can simplify both the loop and the expansion by iterating over an array instead of using variable interpolation. For example:
SAMPLE=('1-first.with.custom.name' '2-second.with.custom.name')
for i in "${SAMPLE[#]}"; do
echo "$i"
done
This has added benefits over the other methods. Specifically:
You don't need to specify a complex loop test.
You access individual array elements via the $SAMPLE[$i] syntax.
You can get the total number of elements with the ${#SAMPLE} variable expansion.
Practical Equivalency for Original Example
All three methods will work for the example given in the original question, but the array solution provides the most overall flexibility. Choose whichever one works best for the data you have on hand.
You can use eval as shown below:
SAMPLE1='1-first.with.custom.name'
SAMPLE2='2-second.with.custom.name'
for (( i = 1; i <= 2; i++ ))
do
eval echo \$SAMPLE$i
done
Not as far as I know, They way #johnshen64 said. Also, you could solve your problem using an array like so:
SAMPLE[1]='1-first.with.custom.name'
SAMPLE[2]='2-second.with.custom.name'
for (( i = 1; i <= 2; i++ )) do
echo ${SAMPLE[$i]}
done
Note that you don't need to use numbers as indexes SAMPLE[hello] will work just as well
Not a standalone answer, just an addition to Miquel's answer which I couldn't fit well in a comment.
You can populate the array using a loop, the += operator, and a here document as well:
SAMPLE=()
while read; do SAMPLE+=("$REPLY"); done <<EOF
1-first.with.custom.name
2-second.with.custom.name
EOF
In bash 4.0, it's as simple as
readarray SAMPLE <<EOF
1-first.with.custom.name
2-second.with.custom.name
EOF
eval "echo $SAMPLE${i}"
Can someone tell me what language(s) the following formula is formatted for?
IF(($A) > $B,$B, IF(($A) < $C,$C,($A)))
I work in PHP and I can interpret what it's intended to do
(if $A > $B then $B, if $A < $C then $C, ELSE $A)
just not the language that would except it.
This looks like something that came out of Excel
in php it should be somthink like this...
<?php
if($A > $B){$B;}
elseif($A < $C){$C;}
else{$A;}
?>
Just following up to close this off.
First, there was a slight typo with an unecessary set of parenthesis in the example so ($A) could've just been $A in both cases.
In any event, I discovered that this was used for something that was originally written in "ABAP".