-
MODX Staff
- 2,502 Posts
Hey all I am writing a custom snippet for a client and I can’t figure out how to get back the values of a loop from a function. I don’t necessarily need to do that but I have a set of 7 and 7 template variable pairs that I have turned into an array to loop through to get a result. Any help would be appreciated.
Here is the snippet:
<?php
/*
#
# RelatedInfo Snippet
# by Jay Gilmore (SmashingRed Web & Marketing)
#
#
*/
// Script Functions
function getLinks(){
// Assign Variable to TV "relatedLinks"
$links = 'relatedLinks';
//Assign global scope to $modx
global $modx;
// Get Value of the RelatedLinks Template Variable
$relatedLink = $modx->getTemplateVarOutput(array($links));
$outLink = $relatedLink[$links];
if (empty($outLink)){
return '';
}
return $outLink;
}
function getFiles(){
// Assign Array to TVs from file1Text to file7Text as Keys
// and file1 to file7 as Values
$file["file1Text"]="file1";
$file["file2Text"]="file2";
$file["file3Text"]="file3";
$file["file4Text"]="file4";
$file["file5Text"]="file5";
$file["file6Text"]="file6";
$file["file7Text"]="file7";
// Assign global scope to $modx
global $modx;
// Loop Through the file array and output all the information
foreach ($file as $key => $value){
$fileName = $modx->getTemplateVarOutput(array($key));
$fileLoc = $modx->getTemplateVarOutput(array($value));
$outFileName = $fileName[$key];
$outFileLoc = $fileLoc[$value];
if (empty($outFileName) || empty($outFileLoc)) {
return '';
}
$outPutFile = "<li><a href=\"".$outFileLoc."\" title=\"Click to download ".$outFileName."\">".$outFileName."</a></li>\n";
$output = $outPutFile;
}
// Return $outPutFile
return $output;
}
// Assign Variables to Functions
$getLinksOutput = getLinks();
$getFilesOutput = getFiles();
// Do the Magic
if (empty($getLinksOutput) && empty($getFilesOutput)){
return false;
}
else {
echo "<div class=\"sidepanel\">\n";
echo "<h5>Related Info</h5>\n";
if (!empty($getLinksOutput)){
print $getLinksOutput;
}
if (!empty($getFilesOutput)){
print "<ul>\n".$getFilesOutput."</ul>\n";
}
echo "</div>\n";
}
?>
The trouble is how to get the value of
getLinks() getFiles() as a loop within the actual output. I am coding above my pay grade here so please help.
Cheers,
Jay
Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup
magician.
Blog ✦
Twitter ✦
LinkedIn ✦
GitHub
Do you have an example of the content that would reside in the TV ’relatedLinks’? And subsequently what you expect to be returned by getLinks()? Is it something comma delimited? Pipe delimited?
Jesse R.
Consider trying something new and extraordinary.
Illinois Wine
Have you considered donating to MODx lately?
Donate now. Every contribution helps.
-
MODX Staff
- 2,502 Posts
Jessie,
The template variables for RelatedLinks are a file path (string) and a file title (string). I have 7 of each so if you look at the $file array you will see key value sets of the fields.
The output will be formated as in the loop so what I will end up with is a list of links that allow people to download the files when clicked.
Cheers,
Jay
Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup
magician.
Blog ✦
Twitter ✦
LinkedIn ✦
GitHub
Maybe I’m misunderstanding what you’re processing, but how about something like:
foreach ($relatedLink as $key => $value) {
$output .='<a href="'.$key.'>';
$output .=$value.'</a><br />'."\n";
}
Bob
-
MODX Staff
- 2,502 Posts
BobRay,
I made an error when I posted. I am trying to get the values of getFiles() which is a loop through the array grabbing the values of the tvs that appear in the $file array.
So what is happening now is that the loop takes the TV name and requests the value via $modx->getTemplateVarOutput(array($key)) and $modx->getTemplateVarOutput(array($value)) and as the loop continues it gets all values from all TVs in the array that have values.
I can use "print $outPutFiles" to get the result from outside the function but I want to get the result and then call it from the output script at the bottom.
Hope this makes sense.
Cheers,
Jay
Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup
magician.
Blog ✦
Twitter ✦
LinkedIn ✦
GitHub
I’m having kind of a foggy day so it’s still a bit hazy to me.
Would it help to have all the files in the same directory and just read the directory? That would give you the filenames and the links anywhere you want them. I know how to do that.
Bob
I feel like what you are trying to do should be relatively easy, but I think we are all lacking a fundamental understand of what you are trying to accomplish. Maybe I am just having a foggy day too!
Jesse R.
Consider trying something new and extraordinary.
Illinois Wine
Have you considered donating to MODx lately?
Donate now. Every contribution helps.
-
MODX Staff
- 2,502 Posts
Bob,
Here is the scenario from start to finish:
The client site is to have MODx Documents with content and each MODx Document has the following TVs
relatedLinks (URL, Hyperlink Widget)
file1 (File, )
file1Text (Text)
file2
file2Text
file3
file3Text
file4
file4Text
file5
file5Text
file6
file6Text
file7
file7Text
Each of the file1 => file1Text pairs have values like: /assets/files/howto.pdf => How To
The idea is to have the snippet act as a widget to check if there is a value in one of the tvs above and then output it formatted in some way, in this case I am formatting the pair as an URL with the key as the URL and Value as the Text.
I can certainly get the value of the foreach statement if I put in within the output but I can’t control its container and so I wanted to call it from a function and ouput to the formatted output that is below the functions.
The point is to allow the client to just upload a file and give it a name and if they exist show them in the Related Info sidebar.
I will continue to play with this but any suggestions would be great. I am pushing my understanding of PHP to the limits and I don’t know if I will fall.
Cheers,
Jay
Author of zero books. Formerly of many strange things. Pairs well with meats. Conversations are magical experiences. He's dangerous around code but a markup
magician.
Blog ✦
Twitter ✦
LinkedIn ✦
GitHub
Call me stubborn, but I can’t see why one single TV + one snippet wouldn’t achieve the same thing as 14 TVs + a snippet.
Just use an RTE TV. Surely the client knows how to insert simple links via tinyMCE? With a simple snippet, you would only have to check if this TV is empty or not. He wouldn’t be limited to only x number of documents then, and processing overhead would be minimal. If the TV is not empty, output the sidebar DIV (surrounding HTML/CSS). Something like [[checkPDFLinks? &tv=`pdflinks`]] would be enough in your template.
This is starting to make me dizzy. I’ve been getting things wrong all day, but I’m going to take a stab at what I think you mean.
The client enters the filename in a form field, specifies the location of the file on his or her machine, and then clicks a button to upload the file.
Then they get redirected to the same page, but this time it shows the newly uploaded file (files?) as links and any other files that have been uploaded through that particular page?
Am I close?
Bob