CakePHP 3.x, How to get full path to a template file - templates

I've tried $this->viewBuilder()->templatePath(), but this only returns the prefix and controller name. (ex. Dashboard/Users)
The full path is more like /usr/local/var/www/mysite/vendor/vendorname/users/src/Template/Dashboard/Users
I've tried a few other things like Plugin::path($this->viewBuilder()->plugin()) to get part of that path, but I have yet to find any piece of code that will return the settings for what the src folder is called and what the Template folder is called.
I could hard code them as 'src' . DS . 'Template', but was hoping I'd find something that would work even if those were changed through some config setting somewhere. (Ideally there would just be a viewBuilder->absoluteTemplatePath() or something like it.)

You can retrieve possible template paths via App::path().
If you want to retrieve the template path for your Users plugin, then you could do
$templatesPath = current(\Cake\Core\App::path('Template', 'Users'));
This would give you something like
/usr/local/var/www/mysite/vendor/vendorname/users/src/Template/
It should be noted that this method doesn't necessarily return only a single path, it does so for plugin templates though.
If you need the path to an actual file, then you'll have to concatenate the remaining path segments on your own.
See also
API > \Cake\Core\App::path()

Related

Human readable file name

Whenever I create an ImageField using a file called moon.png, Django, correctly following my configuration settings, puts the file in:
campaign/primary-banner/2015/11/25/moon.png
or
campaign/primary-banner/2015/11/25/moon_RcJ3FuD.png
And that is the value of imagefield.name, which I can show to the user, but is not really user friendly.
I would like to show the name of ImageField.name, but in a human readable format. Is it possible to extract the original file name (moon.png) from the ImageField? The workarounds that I can think of are:
add an extra field to my model to hold the human readable file name. Extra work, which I would like to avoid: DRY.
process the imagefield.name value to extract the original filename, but this seems too complex (I would need to exactly understand how django is generating the filename in the first place, to make sure I cover corner cases)
I had similar problem and solved it by creating my own subdirectry under MEDIA_ROOT, in which I made directory structure involvind the date and also some unique identifier. Then I moved my file to that final subdirectory and put the name to FileField.name. Everything works like charm - files are unique (thanks to unique directories) and the final name is exactly what i want user to see (as there is no need to rename it - no conflict is possible)
p.filename=get_filename_of_existing_file_to_be_stored()
p.originalfilename=get_how_it_should_be_named()
masterobject=MyNewMaster.objects.get(pk=some_id)
uniq_name=masterobject.make_something_unique()
new_object=MyObject()
new_object.master=masterobject
new_object.some_fields=some_values
daystr=date.today().strftime('%y-%m-%d')
directory='MyDir/%s/%s/%s/'%(daystr,masterobject.id,uniq_name)
if not os.path.exists(os.path.join(settings.MEDIA_ROOT,directory)):
os.makedirs(os.path.join(settings.MEDIA_ROOT,directory))
fname=os.path.join(directory,p.originalfilename)
os.rename(p.filename,os.path.join(settings.MEDIA_ROOT,fname))
new_object.filename = fname
new_object.save()
Now everthing works for me and no conflicts are possible and even Django seems happy with this hack and provides the correct filename and url for everything i tried to do with MyObject :)
Now I ended with path like:
MyDir/2015-11-29/12345/child_7/moon.png

filesyncprovider creates folders for documents with same name as document

I am building a sync app with a customprovider and a filesyncprovider. I based my provider on this example:
https://code.msdn.microsoft.com/File-Sync-with-Simple-c497bf87
Now I want to extend to a hierarchical folderstructure. So in the EnumerateItems method of the custom syncprovider I return all files and folders just like I did before with only the files in the directory. Now on the filesyncprovider side, this results in a creation of folders with the name of the file and the file being placed in this folder. E.g.
Folder1\textfile.txt\textfile.txt
I have no idea, what I am doing wrong and I find it hard to know the part of the MS filesyncprovider where I could debug to see, what's happening.
My question is, what am I doing wrong and how can I correct it, so that the correct output would be
Folder1\textfile.txt?
Best regards,
Tobias
// Must return the relative path without the filename
public string RelativeDirectoryPath
{
get
{
return _relativeFilePath;
}
Read first - then ask: I returned the path to the file instead of the path to the folder... comment above even warns not to do that...

Coldfusion mapping cfinclude

I am trying to use mappings for the first time and I am having some issues. In my CFadmin I created a mapping which goes like this:
Logical Path:
/email_sender
Directory Path:
E:\sites\Example.Com\cf_modules\autoresponders\Emails\emailLists
I am trying to access the include file from a .cfc. Inside my cfc I have an include file which is in the same folder and it works just fine but that include file is tying to access another include file based on the information I am passing through.
The include file inside the component has this code in it.
<cfmail from="#emailData.sender#" to="#surveymain.email#" subject="#subject#" type="HTML" >
<cfinclude template="#emailData.includePath#" >
</cfmail>
Just to be more clear this is my total filesystem:
access.cfm -> some.cfc -> include_1 (works) -> include_2 (issue with
the path)
I have a try/catch which emails me the errors and it is located inside the include_1.
How can I use the mapping that I created to finally be able to access my include file?
Please note that after emailLists I have dynamic folders which change depending on which client I am trying to send the email to. So the end result of the path would be:
E:\sites\Example.Com\cf_modules\autoresponders\Emails\emailLists\client_A\email_template.cfm
I tried using my mapping by doing this
<cfinclude template="/email_sender/#emailData.includePath#" >
which gave me this error:
The path to the CFC must be specified as a full path, or as a relative
path from the current template, without the use of mappings.
Also, I tried including a full path in the cfinclude whic also resulted in the following error:
Note: If you wish to use an absolute template path (for example, >template="/mypath/index.cfm") with CFINCLUDE, you must create a
mapping for the path using the ColdFusion Administrator. Or, you can
use per-application settings to specify mappings specific to this
application by specifying a mappings struct to THIS.mappings in
Application.cfc. Using relative paths (for example,
template="index.cfm" or template="../index.cfm") does not require the
creation of any special mappings. It is therefore recommended that you
use relative paths with CFINCLUDE whenever possible.
Any help/suggestion would be much appreciated
Edit:
After restarting the server the version that worked was this one:
<cfinclude template="/email_sender/#emailData.includePath#" >
Thanks to those who replied.
Some questions, rather than some answers.
Did you set the mapping in CFAdmin or in Application.cfc?
What do you get if you run the code below?
#expandPath("/email_sender")#
What is the value of #emailData.includePath#?
What is the value of this: #expandPath("/email_sender#emailData.includePath#")#?
Can you provide us with the exact text of the error messages, rather than vaguely describing them.
What is the value of #fileExists(expandPath("/email_sender#emailData.includePath#"))#?
If you can update your question with that info, you'll either spot where you're going wrong, or we can help to work out what the story is
Something like this happened to me once before. Essentially we edited the xml file, rather than using the admin interface. It wasn't until our sys admin restarted the server until we realized why the mapping hadn't taken. So have you restarted the server since you added the mapping?

Could not find the included template

When I'd like to run idnex.cfm there is error "Could not find the included template".
I should use CF admin mappings, but there are a lot of such errors in different files.
So, is there the easier way to solve a problem?
Full error message:
Could not find the included template spiderBlock.cfm.
Note: If you wish to use an absolute template path (for example, template="/mypath/index.cfm") with CFINCLUDE, you must create a mapping for the path using the ColdFusion Administrator. Or, you can use per-application settings to specify mappings specific to this application by specifying a mappings struct to THIS.mappings in Application.cfc.
Using relative paths (for example, template="index.cfm" or template="../index.cfm") does not require the creation of any special mappings. It is therefore recommended that you use relative paths with CFINCLUDE whenever possible.
The error should also tell you WHICH template it can't find, including the path CF is looking for it on. Is that file at the location CF is looking for it? I suspect not.
If you start by giving us the full error message, we've got something to work with to make sensible comment on this.
As others have suggested there's a few ways the included file can be referenced in the including template: relative to its own location, absolutely from the CF root, or absolutely from a resolved mapping. So you might need to check your mappings as well (which'll be defined in CFAdmin or Application.cfc).

Magento - locate specific core files

I am familiar with theming and using template hints in the Magento back office to locate .phtml files.
What I am not really familiar with are the core files such as app/code/core/Mage/Catalog/Model
What I need to do is override a core file like I would a core phtml file by copying it to 'my theme'.
I basically want to amend some labels which appear on the order summary page of the Magento checkout process - domain.com/checkout/cart/
I followed the trail to the phtml files using template hints. Within the app/design/frontend/default/mytheme/template/checkout/cart I found the code
renderTotals(); ?>
Now I managed, by accident, to stumble upon two of the files I wanted to change:
/httpdocs/app/code/local/Mage/Sales/Model/Quote/Address/Total/Grand.php
/httpdocs/app/code/local/Mage/Sales/Model/Quote/Address/Total/Shipping.php
I made local copies of these files (http://www.magentocommerce.com/wiki/how_to/how_to_create_a_local_copy_of_app_code_core_mage) to override the default labels, like I would if I was overriding a template file.
My question is, how can you locate core files which pertain to the 'stuff' you want to change, located in function calls such as renderTotals(); ?> in the phtml files?
Not being able to pinpoint stuff like I can with template hints is slowing me down, and I am struggling to find a solution as I am not up on all the vocab surrounding Magento yet.
Hope this makes sense and thanks in advance!
From the same settings page where you turn on Template Path Hints, also turn on the "Add Block Names to Hints" setting. This will show you PHP class names such as: Mage_Sales_Model_Quote_Address_Total_Grand to which you can deduce the folder path (underscores represent a subfolder, and the last piece represents the file name).
If you're getting a block such as Mage_Sales_Model_Quote_Address_Total_Default then sometimes it just takes a little common sense to see that it's pulling in other files from the same folder (such as Grand.php and Shipping.php). But there are generally only a couple files in the same folder, so this is pretty easy to see.
As Sid Vel said, a good Search Project functionality is helpful. But if you find yourself looking at Abstract.php of some class, often you need to look in a subfolder in that directory with the proper name to find the concrete implementations. But still, it gets you very close to where you need to be.
I always use Dreamweaver's site / directory search function. It will scan through all the files in the Core folder and tell you where the function is from. In your case, I would search for "renderTotals". You need to enable PHTML editing in Dreamweaver.
Most IDE's will allow this kind of search option. In Aptana you can Ctrl + Click on the function to open the file it is coming from. Magento takes ages to index itself on Aptana, due to its sheer size.